public function unlinkAll($name, $delete = false)
{
$relation = $this->getRelation($name);
if ($relation->via !== null) {
if (is_array($relation->via)) {
list($viaName, $viaRelation) = $relation->via;
$viaClass = $viaRelation->modelClass;
unset($this->_related[$viaName]);
} else {
$viaRelation = $relation->via;
$viaTable = reset($relation->via->from);
}
$condition = [];
$nulls = [];
foreach ($viaRelation->link as $a => $b) {
$nulls[$a] = null;
$condition[$a] = $this->$b;
}
if (!empty($viaRelation->where)) {
$condition = ['and', $condition, $viaRelation->where];
}
if (property_exists($viaRelation, 'on') && !empty($viaRelation->on)) {
$condition = ['and', $condition, $viaRelation->on];
}
if (is_array($relation->via)) {
if ($delete) {
$viaClass::deleteAll($condition);
} else {
$viaClass::updateAll($nulls, $condition);
}
} else {
$command = static::getDb()->createCommand();
if ($delete) {
$command->delete($viaTable, $condition)->execute();
} else {
$command->update($viaTable, $nulls, $condition)->execute();
}
}
} else {
$relatedModel = $relation->modelClass;
if (!$delete && count($relation->link) === 1 && is_array($this->{$b = reset($relation->link)})) {
$this->$b = [];
$this->save(false);
} else {
$nulls = [];
$condition = [];
foreach ($relation->link as $a => $b) {
$nulls[$a] = null;
$condition[$a] = $this->$b;
}
if (!empty($relation->where)) {
$condition = ['and', $condition, $relation->where];
}
if (property_exists($relation, 'on') && !empty($relation->on)) {
$condition = ['and', $condition, $relation->on];
}
if ($delete) {
$relatedModel::deleteAll($condition);
} else {
$relatedModel::updateAll($nulls, $condition);
}
}
}
unset($this->_related[$name]);
}
在下一个锚点
https://yiiframework.cn/doc/api/2.0/yii-db-activerecord#hasMany()-detail
下一段文字出现了两次
有关更多信息,请参阅 yii\db\BaseActiveRecord::hasMany()
在下一个锚点
https://yiiframework.cn/doc/api/2.0/yii-db-activerecord#hasOne()-detail
下一段文字出现了两次
有关更多信息,请参阅 yii\db\BaseActiveRecord::hasOne()
注册 或 登录 以便评论。