Browse Source

AR WIP

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
6243861138
  1. 27
      framework/db/ActiveRecord.php
  2. 2
      framework/db/ActiveRelation.php

27
framework/db/ActiveRecord.php

@ -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);
}
}
} }

2
framework/db/ActiveRelation.php

@ -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
*/ */

Loading…
Cancel
Save