mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进多对多的attach方法的返回值
This commit is contained in:
@@ -54,7 +54,7 @@ class BelongsToMany extends Relation
|
|||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
$middle = $this->middle;
|
$middle = $this->middle;
|
||||||
if ($closure) {
|
if ($closure) {
|
||||||
call_user_func_array($closure, [& $this->query]);
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
}
|
}
|
||||||
// 关联查询
|
// 关联查询
|
||||||
$pk = $this->parent->getPk();
|
$pk = $this->parent->getPk();
|
||||||
@@ -174,8 +174,8 @@ class BelongsToMany extends Relation
|
|||||||
return $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, [
|
return $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, [
|
||||||
'pivot.' . $this->localKey => [
|
'pivot.' . $this->localKey => [
|
||||||
'exp',
|
'exp',
|
||||||
'=' . $this->parent->getTable() . '.' . $this->parent->getPk()
|
'=' . $this->parent->getTable() . '.' . $this->parent->getPk(),
|
||||||
]
|
],
|
||||||
])->fetchSql()->count();
|
])->fetchSql()->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,7 +271,7 @@ class BelongsToMany extends Relation
|
|||||||
* @access public
|
* @access public
|
||||||
* @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键
|
* @param mixed $data 数据 可以使用数组、关联模型对象 或者 关联对象的主键
|
||||||
* @param array $pivot 中间表额外数据
|
* @param array $pivot 中间表额外数据
|
||||||
* @return int
|
* @return array|Pivot
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function attach($data, $pivot = [])
|
public function attach($data, $pivot = [])
|
||||||
@@ -301,7 +301,12 @@ class BelongsToMany extends Relation
|
|||||||
$ids = (array) $id;
|
$ids = (array) $id;
|
||||||
foreach ($ids as $id) {
|
foreach ($ids as $id) {
|
||||||
$pivot[$this->foreignKey] = $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;
|
return $result;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user