Commit 4d049fd9 by Qiang Xue

AR WIP

parent 211c3fb8
...@@ -95,7 +95,9 @@ class ActiveQuery extends BaseQuery ...@@ -95,7 +95,9 @@ class ActiveQuery extends BaseQuery
$class = $this->modelClass; $class = $this->modelClass;
$model = $class::create($row); $model = $class::create($row);
if (!empty($this->with)) { if (!empty($this->with)) {
$this->fetchRelatedModels(array($model), $this->with); $models = array($model);
$this->fetchRelatedModels($models, $this->with);
$model = $models[0];
} }
return $model; return $model;
} }
......
...@@ -39,9 +39,25 @@ class ActiveRelation extends ActiveQuery ...@@ -39,9 +39,25 @@ class ActiveRelation extends ActiveQuery
*/ */
public $link; public $link;
/** /**
* @var ActiveRelation * @var array
*/ */
public $via; public $via;
/**
* @var array
*/
public $viaTable;
public function via($modelClass, $properties = array())
{
$this->via = array($modelClass, $properties);
return $this;
}
public function viaTable($tableName, $link, $properties = array())
{
$this->viaTable = array($tableName, $link, $properties);
return $this;
}
public function createCommand($db = null) public function createCommand($db = null)
{ {
...@@ -53,9 +69,13 @@ class ActiveRelation extends ActiveQuery ...@@ -53,9 +69,13 @@ class ActiveRelation extends ActiveQuery
public function findWith($name, &$primaryModels) public function findWith($name, &$primaryModels)
{ {
if (empty($this->link) || !is_array($this->link)) { if (!is_array($this->link)) {
throw new \yii\base\Exception('invalid link'); throw new \yii\base\Exception('invalid link');
} }
if ($this->via !== null) {
}
$this->filterByPrimaryModels($primaryModels); $this->filterByPrimaryModels($primaryModels);
if (count($primaryModels) === 1 && !$this->multiple) { if (count($primaryModels) === 1 && !$this->multiple) {
...@@ -64,28 +84,32 @@ class ActiveRelation extends ActiveQuery ...@@ -64,28 +84,32 @@ class ActiveRelation extends ActiveQuery
} }
} else { } else {
$models = $this->all(); $models = $this->all();
// distribute models into buckets which are indexed by the link keys $this->bindModels($name, $primaryModels, $models);
$buckets = array(); }
foreach ($models as $i => $model) { }
$key = $this->getModelKey($model, array_keys($this->link));
if ($this->index !== null) { protected function bindModels($name, &$primaryModels, $models)
$buckets[$key][$i] = $model; {
} else { $buckets = array();
$buckets[$key][] = $model; foreach ($models as $i => $model) {
} $key = $this->getModelKey($model, array_keys($this->link));
if ($this->index !== null) {
$buckets[$key][$i] = $model;
} else {
$buckets[$key][] = $model;
} }
if (!$this->multiple) { }
foreach ($buckets as $i => $bucket) { if (!$this->multiple) {
$buckets[$i] = reset($bucket); foreach ($buckets as $i => $bucket) {
} $buckets[$i] = reset($bucket);
} }
foreach ($primaryModels as $i => $primaryModel) { }
$key = $this->getModelKey($primaryModel, array_values($this->link)); foreach ($primaryModels as $i => $primaryModel) {
if (isset($buckets[$key])) { $key = $this->getModelKey($primaryModel, array_values($this->link));
$primaryModels[$i][$name] = $buckets[$key]; if (isset($buckets[$key])) {
} else { $primaryModels[$i][$name] = $buckets[$key];
$primaryModels[$i][$name] = $this->multiple ? array() : null; } else {
} $primaryModels[$i][$name] = $this->multiple ? array() : null;
} }
} }
} }
......
...@@ -29,7 +29,7 @@ class Order extends ActiveRecord ...@@ -29,7 +29,7 @@ class Order extends ActiveRecord
public function books() public function books()
{ {
return $this->hasMany('Item', array('id' => 'item_id')) return $this->hasMany('Item', array('id' => 'item_id'))
->via('tbl_order_item', array('order_id' => 'id')) ->viaTable('tbl_order_item', array('order_id' => 'id'))
->where(array('category_id' => 1)); ->where(array('category_id' => 1));
} }
} }
\ No newline at end of file
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