SQL Server数据库错误中的Laravel UUID

时间:2018-12-03 14:14:19

标签: sql-server laravel eloquent uuid

我在使用现有数据库(使用MS SQL UUID)的Laravel应用程序中遇到问题。我的应用程序有一个客户:

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表,最终用户可以使用该表自定义Customer实体:

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');
    }
}

CustomerController:

 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用于将Customer表与Customer_UD链接表格

Customer_UD的示例行是:

enter image description here

我的问题是,当尝试将UD字段与Customer字段一起返回时,出现错误:

SQLSTATE[HY000]: General error: 20018 Incorrect syntax near ''. 
[20018] (severity 15) [select * from [ERP].[Customer_UD] where [ERP]. 
[Customer_UD].[ForeignSysRowID] = '���_�X�O�Q׊3�^w' and [ERP]. 
[Customer_UD].[ForeignSysRowID] is not null] 

我认为这很奇怪,因此我赞扬了CustomerController中的Customer_UD行,只是尝试在显示刀片中显示Customer UUID字段:

SysRowID: {{$Customer->SysRowID}}

我什么也没有,没有错误但没有数据。我为Customer_UD模型创建了一个控制器和索引刀片,并且可以显示除UUID字段之外的所有Customer_UD数据库字段。

我实际上并不想显示UUID字段-但确实需要使用它们来建立关系。谁能帮助我指出正确的方向?

1 个答案:

答案 0 :(得分:0)

我发现添加:

'options' => [
PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER => true,
],

到config \ database.php中的数据库配置解决了该问题。