Commit 62438611 by Qiang Xue

AR WIP

parent c9b104f0
...@@ -856,5 +856,30 @@ abstract class ActiveRecord extends Model ...@@ -856,5 +856,30 @@ abstract class ActiveRecord extends Model
return $this->__isset($offset); return $this->__isset($offset);
} }
/**
* @param string $name
* @param ActiveRecord $model
* @throws Exception
*/
public function linkWith($name, $model)
{
$getter = 'get' . $name;
if (!method_exists($this, $getter)) {
throw new Exception('Unknown relation: ' . $name);
}
$relation = $this->$getter();
if (!$relation instanceof ActiveRelation) {
throw new Exception('Unknown relation: ' . $name);
}
if ($relation->multiple) {
foreach ($relation->link as $a => $b) {
$key = $this->$b;
if ($key === null) {
throw new Exception('key null');
}
$model->$a = $this->$b;
}
return $model->save(false);
}
}
} }
...@@ -41,7 +41,7 @@ class ActiveRelation extends ActiveQuery ...@@ -41,7 +41,7 @@ class ActiveRelation extends ActiveQuery
* must be the corresponding columns from the primary table. * must be the corresponding columns from the primary table.
* Do not prefix or quote the column names as they will be done automatically by Yii. * Do not prefix or quote the column names as they will be done automatically by Yii.
*/ */
protected $link; public $link;
/** /**
* @var array|ActiveRelation * @var array|ActiveRelation
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment