【发布时间】:2021-10-29 23:18:30
【问题描述】:
我正在使用 Livewire 编写内容可编辑的数据表。该数据库是一个外部 MS SQL Server。
特别是在这种情况下,我得到了一个users 数据表,其中的这一列来自belongsTo() 关系:areas 表。在此示例中,用户在(区域列的)行上单击鼠标右键,然后呈现选择区域组件。当用户单击一个选项时,livewire 会触发一个更新方法并使用新值重新渲染表(我实际上假装要实现的)。但是,就在保存模型的那一刻。我得到了那个错误。
这是我的代码: 用户.php
class User extends Model
{
use Notifiable;
/**
* External DB connection on MSSQL SERVER.
*
* @var string
*/
protected $connection='ext'; //external MS SQL Server
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/************
******************** R E L A T I O N S H I P S ********************
***********/
public function area()
{
return $this->belongsTo(Area::class,'area_id');
}
}
Area.php
class Area extends Model
{
/**
* External DB connection on MSSQL SERVER
*
* @var string
*/
protected $connection='ext';//external MS SQL Server
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
}
当您单击该列的特定行时,livewire 会调用另一个呈现选择选项的组件。
<td wire:click.prevent="editAreaColumn({{$record->area->id}},{{$record->id}})" class="px-6 py-4">
@if($identifier!='' && $identificador==$record->id){{-- && $editAreaId==$record->area->id--}}
@livewire('path.to.select-area',['area_id'=>$record->area->id,'record_id'=>$record->id])
@elseif($identifier == '' && $identifier != $record->id){{--&& $editAreaId=='' && $editAreaId!=$record->area->id--}}
<div class="text-sm text-gray-900">{{$record->area->name}}</div>
@else
<div class="text-sm text-gray-700">{{$record->area->name}}</div>
@endif
</td>
到目前为止一切顺利。我已经编程,所以当你点击一个选项时,livewire 会触发一个更新方法。在 HTML select 刀片组件中,我得到了以下内容:
<div wire:init="loadData">
<div class="m-2 relative">
@if($readyToLoad)
@if($areas->count()>0)
<select class="text-xs appearance-none border border-gray-200 py-1 px-3 w-full truncate rounded-lg" id="area_id">
<option value="" selected hidden>Choose</option>
@foreach($areas as $area)
<option wire:click="updateArea({{$area->id}})" value="{{ $area->id }}">{{ $area->name }}</option>
@endforeach
</select>
<div class="border-gray-400 border-r border-b p-1 absolute top-0 right-0 transform rotate-45 mt-2.5 mr-3 pointer-events-none"></div>
@else
<div
class=" block text-xs text-left text-white bg-red-500 h-5 flex items-center p-1 rounded-md"
role="alert"
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="w-6 h-6 mx-1 stroke-current"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
></path>
</svg>
No results
</div>
@endif
@else
{{--Loading spinner, https://tailwindcss.com/docs/animation--}}
@endif
</div>
</div>
然后在 HTML select 类组件中,我得到以下不工作的内容:
public function updateArea($new_area_id){
$this->new_area_id = $new_area_id;
$this->validate();
$user = User::findOrFail($this->record_id);
$area = Area::findOrFail($new_area_id);
$user->area_id=$area->id;
$user->save();//ERROR TRIGGERS HERE: General error 20018 UPDATE failed because the following SET options have incorrect settings
$this->emit('areaUpdated');
}
错误是:
SQLSTATE[HY000]:一般错误:20018 UPDATE 失败,因为 以下 SET 选项的设置不正确:'ANSI_NULLS, CONCAT_NULL_YIELDS_NULL、ANSI_WARNINGS、ANSI_PADDING'。验证 SET 选项对于与索引视图和/或索引一起使用是正确的 计算列和/或过滤索引和/或查询通知 和/或 XML 数据类型方法和/或空间索引操作。 [20018] (严重性 16)[更新 [用户] 设置 [area_id] = 6 其中 [id] = 4](SQL: 更新 [用户] 设置 [area_id] = 6 其中 [id] = 4)
这是数据库连接的配置:
config/database.php
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
这个错误很奇怪,因为在另一个 areas 数据表中,我实现了一个类似的内联内容可编辑数据表,所有 CRUD(插入、更新和删除)都可以正常工作。
在另一种情况下,我得到了这个 areas 数据表,其中我有明确的按钮来更新和删除(这与我的第一个数据表不同,用户可以直接单击行进行编辑)。
当用户点击编辑图标时,区域名称变为可编辑输入,然后出现save 和cancel 按钮。
如果用户点击保存,
<td class="px-6 py-4 text-right text-sm font-medium">{{-- whitespace-nowrap--}}
@if($editedInputId == $area->id)
<div class="flex items-end">
<div class="flex items-end">
<button class="inline-flex items-center px-6 py-2 m-2 px-2 py-2 bg-blue-600 text-white rounded-lg shadow-sm hover:bg-blue-500 focus:ring-2 focus:ring-blue-200" wire:click.prevent="updateArea({{$area->id}})" wire:loading.attr="disabled" wire:target="updateArea" class="disabled:opacity-25">
<div wire:loading wire:target="updateArea" style="border-top-color:transparent" class="w-4 h-4 border-4 border-gray-300 border-solid rounded-full animate-spin"></div>
<span class="ml-2">SAVE</span>
</button>
<x-jet-secondary-button wire:click="cancelEdit" class="ml-2">CANCEL</x-jet-secondary-button>
</div>
</div>
{{-- ... --}}
一个更新区域方法被触发
public function updateArea($editedInputId){
$this->validate();
$area = Area::findOrFail($editedInputId);
$area->name = $this->name;
$area->save();
$this->empty();
}
并且更新过程完美运行。
如何修复第一个数据表中的错误?我错过了什么?我应该尝试什么?有什么解决方法吗?
【问题讨论】:
标签: sql-server laravel-livewire