我建议你以下方法:
- 假设您的关系名称与嵌套对象的属性名称相同(调用
$model->link() 方法需要一些规则)
- 为具有嵌套模型的模型声明公共类(例如 ActiveRecordWithNestedModels)
- 在公共类方法
save 和validate 中重写以对这些操作执行级联(使用反射)
- 让你的模型继承这个通用类
或者,作为覆盖validate 方法的替代方案,您可以在公共类中为rules 方法构建一些合适的实现。
这个通用类可以如下所示(这是一个简单的草稿,没有经过测试,只是为了展示概念):
<?php
namespace app\models;
use yii\db\ActiveRecord;
class ActiveRecordWithNestedModels extends ActiveRecord
{
public function save($runValidation = true, $attributeNames = null)
{
$saveResult = parent::save($runValidation, $attributeNames);
$class = new \ReflectionClass($this);
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$propertyValue = $property->getValue($this);
if (!empty($propertyValue) && is_subclass_of($propertyValue, ActiveRecord::className())) {
/* @var ActiveRecord $nestedModel */
$nestedModel = $propertyValue;
$nestedModel->save($runValidation);
$relation = $property->name;
$this->link($relation, $nestedModel);
}
}
return $saveResult;
}
public function validate($attributeNames = null, $clearErrors = true)
{
$class = new \ReflectionClass($this);
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$propertyValue = $property->getValue($this);
if (!empty($propertyValue) && is_subclass_of($propertyValue, ActiveRecord::className())) {
/* @var ActiveRecord $nestedModel */
$nestedModel = $propertyValue;
if (!$nestedModel->validate(null, $clearErrors)) {
array_push($this->errors, [
$property->name => $nestedModel->errors
]);
}
}
}
parent::validate($attributeNames, $clearErrors);
if ($this->hasErrors()) return false;
return true;
}
}
那么你的模型可以是这样的:
class Heart extends ActiveRecordWithNestedModels
{
}
class Human extends ActiveRecordWithNestedModels
{
/* @var Heart $heart */
public $heart = null;
/**
* The relation name will be 'heart', same as property `heart'
*
* @return \yii\db\ActiveQuery
*/
public function getHeart()
{
return $this->hasOne(Heart::className(), ['id', 'heart_id']);
}
}
而且(理论上)你可以这样做:
$human = new Human();
$human->heart = new Heart();
$human->save();
P.S.这里可以有很多复杂的细节在进一步的实现中,比如
- 如果某些子对象保存失败,使用事务回滚
save
- 覆盖
delete
- 服务于
one-to-many 和many-to-many 关系
- 如果属性没有对应关系则跳过级联
- 在级联操作中为
$attributeNames 提供服务
- 等