[top-think/think]使用一对一关联查询时本表数据不能为空

2024-01-31 819 views
8

thinkPHP 5.1.36版本中使用关联查询 有model层的 OrderMaster.php public function send(){ return $this->hasOne('Pharmacy','id','send_pharmacy'); } 此处为关联模型,数据库中有order_master表通过send_pharmacy关联pharmacy表中的选项,业务要求send_pharmacy值可以为null,此时当send_pharmacy值为null时,如果使用with进行关联查询,会出现异常

解决方法: think\model\relation\HasOne.php文件中,修改eagerlyOne函数中的 $data = $this->eagerlyWhere([ [$foreignKey, '=', $result->$localKey], ], $foreignKey, $relation, $subRelation, $closure); 为; if($result->$localKey != null) $data = $this->eagerlyWhere([ [$foreignKey, '=', $result->$localKey], ], $foreignKey, $relation, $subRelation, $closure);

回答

0

这个问题最新版本好像改进过的

9

好的,更新版本后问题已经解决