一对一关联定义支持指定 join类型 用于预载入查询

This commit is contained in:
thinkphp
2016-06-09 17:19:13 +08:00
parent fb5f93f429
commit 34a08232aa
3 changed files with 15 additions and 7 deletions

View File

@@ -41,6 +41,8 @@ class Relation
protected $localKey;
// 数据表别名
protected $alias;
// 当前关联的JOIN类型
protected $joinType;
/**
* 架构函数
@@ -396,15 +398,17 @@ class Relation
* @param string $foreignKey 关联外键
* @param string $localKey 关联主键
* @param array $alias 别名定义
* @param string $joinType JOIN类型
* @return $this
*/
public function hasOne($model, $foreignKey, $localKey, $alias)
public function hasOne($model, $foreignKey, $localKey, $alias = [], $joinType = 'INNER')
{
$this->type = self::HAS_ONE;
$this->model = $model;
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
$this->alias = $alias;
$this->joinType = $joinType;
// 返回关联的模型对象
return $this;
@@ -417,9 +421,10 @@ class Relation
* @param string $foreignKey 关联外键
* @param string $otherKey 关联主键
* @param array $alias 别名定义
* @param string $joinType JOIN类型
* @return $this
*/
public function belongsTo($model, $foreignKey, $otherKey, $alias)
public function belongsTo($model, $foreignKey, $otherKey, $alias = [], $joinType = 'INNER')
{
// 记录当前关联信息
$this->type = self::BELONGS_TO;
@@ -427,6 +432,7 @@ class Relation
$this->foreignKey = $foreignKey;
$this->localKey = $otherKey;
$this->alias = $alias;
$this->joinType = $joinType;
// 返回关联的模型对象
return $this;