修正BELONGS_TO关联查询

This commit is contained in:
thinkphp
2016-04-23 20:26:26 +08:00
parent 8cca5524a7
commit 265a80905d
2 changed files with 16 additions and 9 deletions

View File

@@ -729,17 +729,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* BELONGS TO 关联定义
* @access public
* @param string $model 模型名
* @param string $localKey 关联主键
* @param string $foreignKey 关联外键
* @param string $otherKey 关联主键
* @return \think\db\Query|string
*/
public function belongsTo($model, $localKey = '', $foreignKey = '')
public function belongsTo($model, $foreignKey = '', $otherKey = '')
{
// 记录当前关联信息
$model = $this->parseModel($model);
$foreignKey = $foreignKey ?: $this->pk;
$localKey = $localKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id';
return $this->relation->belongsTo($model, $foreignKey, $localKey);
$foreignKey = $foreignKey ?: Loader::parseName(basename(str_replace('\\', '/', $model))) . '_id';
$otherKey = $otherKey ?: (new $model)->getPk();
return $this->relation->belongsTo($model, $foreignKey, $otherKey);
}
/**