我正在使用 Laravel eloquent ORM,并且定义了一些关系,如下所示:
class Item extends Model
{
protected $table = 'IV00102';
protected $primaryKey = 'ITEMNMBR';
protected $trimmableAttributes = ['ITEMNMBR', 'PRIMVNDR'];
public function backorderQuantities(){
return $this->hasOne(BackorderQuantity::class, 'ITEMNMBR', 'ITEMNMBR')->where('SOPTYPE', 5);
}
}
关系模型为:
class BackorderQuantity extends Model
{
protected $table = 'SOP10200';
protected $primaryKey = 'ITEMNMBR';
protected $trimmableAttributes = ['ITEMNMBR', 'SOPNUMBE', 'SOPTYPE', 'QUANTITY', 'QTYREMAI'];
public function item(){
return $this->belongsTo(Item::class, 'ITEMNMBR', 'ITEMNMBR');
}
}
当我尝试使用定义的关系获取数据时,问题就出现了:
Item::with('backorderQuantities')->where('PRIMVNDR', Auth::user()->vendor_id)
这样做会导致此错误:
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Conversion failed when converting the varchar value 'X123RECORD231' to data type int
我可以完成这项工作,而无需预先加载执行 2 个查询,然后循环并检查关系,如下所示:
foreach ($objects as $view) {
if($view->backorderQuantities){
// do things
}
}
但这给我带来了很多问题,而且更像是创可贴而不是合法的解决方案。我想知道是否有解决此问题的方法,或者 SQL 服务器和 MSSQL 服务器之间的兼容性是否无法实现。
Smart猫小萌