top-think / think

ThinkPHP Framework ——十年匠心的高性能PHP框架
http://www.thinkphp.cn
Other
7.83k stars 1.62k forks source link

使用一对一关联查询时本表数据不能为空 #963

Closed 123shang60 closed 5 years ago

123shang60 commented 5 years ago

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

liu21st commented 5 years ago

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

123shang60 commented 5 years ago

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