内核更新

This commit is contained in:
2017-02-06 17:22:50 +08:00
parent 5e41d6862a
commit 689376a5d9
13 changed files with 213 additions and 91 deletions

View File

@@ -54,7 +54,7 @@ class BelongsToMany extends Relation
$localKey = $this->localKey;
$middle = $this->middle;
if ($closure) {
call_user_func_array($closure, [& $this->query]);
call_user_func_array($closure, [ & $this->query]);
}
// 关联查询
$pk = $this->parent->getPk();
@@ -174,8 +174,8 @@ class BelongsToMany extends Relation
return $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, [
'pivot.' . $this->localKey => [
'exp',
'=' . $this->parent->getTable() . '.' . $this->parent->getPk()
]
'=' . $this->parent->getTable() . '.' . $this->parent->getPk(),
],
])->fetchSql()->count();
}
@@ -271,7 +271,7 @@ class BelongsToMany extends Relation
* @access public
* @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键
* @param array $pivot 中间表额外数据
* @return int
* @return array|Pivot
* @throws Exception
*/
public function attach($data, $pivot = [])
@@ -301,7 +301,12 @@ class BelongsToMany extends Relation
$ids = (array) $id;
foreach ($ids as $id) {
$pivot[$this->foreignKey] = $id;
$result = $this->query->table($this->middle)->insert($pivot, true);
$this->query->table($this->middle)->insert($pivot, true);
$result[] = new Pivot($pivot, $this->middle);
}
if (count($result) == 1) {
// 返回中间表模型对象
$result = $result[0];
}
return $result;
} else {

View File

@@ -45,7 +45,7 @@ class HasMany extends Relation
public function getRelation($subRelation = '', $closure = null)
{
if ($closure) {
call_user_func_array($closure, [& $this->query]);
call_user_func_array($closure, [ & $this->query]);
}
return $this->relation($subRelation)->select();
}
@@ -125,7 +125,7 @@ class HasMany extends Relation
$count = 0;
if (isset($result->$localKey)) {
if ($closure) {
call_user_func_array($closure, [& $this->query]);
call_user_func_array($closure, [ & $this->query]);
}
$count = $this->query->where([$this->foreignKey => $result->$localKey])->count();
}
@@ -141,14 +141,14 @@ class HasMany extends Relation
public function getRelationCountQuery($closure)
{
if ($closure) {
call_user_func_array($closure, [& $this->query]);
call_user_func_array($closure, [ & $this->query]);
}
return $this->query->where([
$this->foreignKey => [
'exp',
'=' . $this->parent->getTable() . '.' . $this->parent->getPk()
]
'=' . $this->parent->getTable() . '.' . $this->parent->getPk(),
],
])->fetchSql()->count();
}
@@ -167,7 +167,7 @@ class HasMany extends Relation
$foreignKey = $this->foreignKey;
// 预载入关联查询 支持嵌套预载入
if ($closure) {
call_user_func_array($closure, [& $model]);
call_user_func_array($closure, [ & $model]);
}
$list = $model->where($where)->with($subRelation)->select();
@@ -217,13 +217,14 @@ class HasMany extends Relation
* @param string $operator 比较操作符
* @param integer $count 个数
* @param string $id 关联表的统计字段
* @param string $joinType JOIN类型
* @return Query
*/
public function has($operator = '>=', $count = 1, $id = '*')
public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER')
{
$table = $this->query->getTable();
return $this->parent->db()->alias('a')
->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $this->joinType)
->join($table . ' b', 'a.' . $this->localKey . '=b.' . $this->foreignKey, $joinType)
->group('b.' . $this->foreignKey)
->having('count(' . $id . ')' . $operator . $count);
}