Laravel 来自 SQL 服务器数据库错误的 UUID
Laravel UUID from SQL Server Database Errors
我在使用现有数据库的 Laravel 应用程序时遇到问题,其中使用了 MS SQL UUID。我的应用程序有一个客户:
class Customer extends Model
{
protected $table = 'ERP.Customer';
public $timestamps = false;
protected $primaryKey = 'CustID';
protected $keyType = 'string';
protected $fillable = [
'CustID',
'SysRowID',
'CustNum',
'LegalName',
'ValidPayer',
'TerritoryID',
'Address1',
'Address2',
'Address3',
'City',
'State',
'Zip',
'Country',
'SalesRepCode',
'CurrencyCode',
'TermsCode',
'CreditHold',
'FaxNum',
'PhoneNum',
'CustomerType'
];
public function SalesTer()
{
return $this->belongsTo(SalesTer::class,'TerritoryID', 'TerritoryID');
}
public function Shipments()
{
return $this->hasMany(Shipment::class, 'CustNum', 'CustNum');
}
public function Equipments()
{
return $this->hasMany(Equipment::class,'CustNum', 'CustNum');
}
public function Customer_UD()
{
return $this->hasOne(Customer_UD::class,'ForeignSysRowID', 'SysRowID');
}
}
其中(在本机 ERP 应用程序中)有一个 UD table,最终用户可以使用它来自定义客户实体:
class Customer_UD extends Model
{
protected $table = 'ERP.Customer_UD';
protected $primaryKey = 'ForeignSysRowID';
public $timestamps = false;
public $incrementing = false;
protected $keyType = 'string';
protected $fillable = [
'ForeignSysRowID',
'MakesCans_c',
'MakesEnds_c',
'Industry_c'
];
public function Customer()
{
return $this->hasOne(Customer::class,'SysRowID', 'ForeignSysRowID');
}
}
客户控制器:
public function show($CustID)
{
if(Customer::find($CustID))
{
$Customer = Customer::find($CustID);
$Customer_UD = $Customer->Customer_UD()
->get();
$Shipments = $Customer->Shipments()
->where('Voided', '0')
->get();
$Equipments = $Customer->Equipments()
->with('Part') // load the Part too in a single query
->where('SNStatus', 'SHIPPED')
->get();
return view('Customer.show', ['NoCust' => '0'],
compact('Equipments', 'Customer','Shipments', 'Parts', 'Customer_UD'));
}
else
{
return view('Customer.show', ['NoCust' => '1']);
}
}
客户有(无论出于何种原因)一个 CustID(人们用它来指代客户)一个 CustNum(不在数据库外部使用)和一个 SysRowID。SysRowID 用于 link客户 table 与 Customer_UD table。
来自 Customer_UD 的示例行是:
我的问题是,在尝试 return UD 字段和客户字段时出现错误:
SQLSTATE[HY000]: General error: 20018 Incorrect syntax near ''.
[20018] (severity 15) [select * from [ERP].[Customer_UD] where [ERP].
[Customer_UD].[ForeignSysRowID] = '���_�X�O�Q3�^w' and [ERP].
[Customer_UD].[ForeignSysRowID] is not null]
我认为这很奇怪,所以我推荐了 CustomerController 中的 Customer_UD 行,并简单地尝试在节目 blade:
中显示 Customer UUID 字段
SysRowID: {{$Customer->SysRowID}}
我什么也没得到,没有错误,但没有数据。我为 Customer_UD 模型创建了一个控制器和索引 blade,并且可以显示除 UUID 字段之外的所有 Customer_UD 数据库字段。
我实际上并不想显示 UUID 字段 - 但确实需要使用它们来建立关系。谁能帮我指出正确的方向?
我发现添加:
'options' => [
PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER => true,
],
到config\database.php中的数据库配置解决了这个问题。
我在使用现有数据库的 Laravel 应用程序时遇到问题,其中使用了 MS SQL UUID。我的应用程序有一个客户:
class Customer extends Model
{
protected $table = 'ERP.Customer';
public $timestamps = false;
protected $primaryKey = 'CustID';
protected $keyType = 'string';
protected $fillable = [
'CustID',
'SysRowID',
'CustNum',
'LegalName',
'ValidPayer',
'TerritoryID',
'Address1',
'Address2',
'Address3',
'City',
'State',
'Zip',
'Country',
'SalesRepCode',
'CurrencyCode',
'TermsCode',
'CreditHold',
'FaxNum',
'PhoneNum',
'CustomerType'
];
public function SalesTer()
{
return $this->belongsTo(SalesTer::class,'TerritoryID', 'TerritoryID');
}
public function Shipments()
{
return $this->hasMany(Shipment::class, 'CustNum', 'CustNum');
}
public function Equipments()
{
return $this->hasMany(Equipment::class,'CustNum', 'CustNum');
}
public function Customer_UD()
{
return $this->hasOne(Customer_UD::class,'ForeignSysRowID', 'SysRowID');
}
}
其中(在本机 ERP 应用程序中)有一个 UD table,最终用户可以使用它来自定义客户实体:
class Customer_UD extends Model
{
protected $table = 'ERP.Customer_UD';
protected $primaryKey = 'ForeignSysRowID';
public $timestamps = false;
public $incrementing = false;
protected $keyType = 'string';
protected $fillable = [
'ForeignSysRowID',
'MakesCans_c',
'MakesEnds_c',
'Industry_c'
];
public function Customer()
{
return $this->hasOne(Customer::class,'SysRowID', 'ForeignSysRowID');
}
}
客户控制器:
public function show($CustID)
{
if(Customer::find($CustID))
{
$Customer = Customer::find($CustID);
$Customer_UD = $Customer->Customer_UD()
->get();
$Shipments = $Customer->Shipments()
->where('Voided', '0')
->get();
$Equipments = $Customer->Equipments()
->with('Part') // load the Part too in a single query
->where('SNStatus', 'SHIPPED')
->get();
return view('Customer.show', ['NoCust' => '0'],
compact('Equipments', 'Customer','Shipments', 'Parts', 'Customer_UD'));
}
else
{
return view('Customer.show', ['NoCust' => '1']);
}
}
客户有(无论出于何种原因)一个 CustID(人们用它来指代客户)一个 CustNum(不在数据库外部使用)和一个 SysRowID。SysRowID 用于 link客户 table 与 Customer_UD table。
来自 Customer_UD 的示例行是:
我的问题是,在尝试 return UD 字段和客户字段时出现错误:
SQLSTATE[HY000]: General error: 20018 Incorrect syntax near ''.
[20018] (severity 15) [select * from [ERP].[Customer_UD] where [ERP].
[Customer_UD].[ForeignSysRowID] = '���_�X�O�Q3�^w' and [ERP].
[Customer_UD].[ForeignSysRowID] is not null]
我认为这很奇怪,所以我推荐了 CustomerController 中的 Customer_UD 行,并简单地尝试在节目 blade:
中显示 Customer UUID 字段SysRowID: {{$Customer->SysRowID}}
我什么也没得到,没有错误,但没有数据。我为 Customer_UD 模型创建了一个控制器和索引 blade,并且可以显示除 UUID 字段之外的所有 Customer_UD 数据库字段。
我实际上并不想显示 UUID 字段 - 但确实需要使用它们来建立关系。谁能帮我指出正确的方向?
我发现添加:
'options' => [
PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER => true,
],
到config\database.php中的数据库配置解决了这个问题。