Browse Source

Fixed a via query bug.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
ac9b315988
  1. 38
      framework/db/ActiveRelation.php

38
framework/db/ActiveRelation.php

@ -49,21 +49,17 @@ class ActiveRelation extends ActiveQuery
/** /**
* @param string $relationName * @param string $relationName
* @param array|\Closure $options * @param callback $callback
* @return ActiveRelation * @return ActiveRelation
*/ */
public function via($relationName, $options = null) public function via($relationName, $callback = null)
{ {
/** @var $relation ActiveRelation */ /** @var $relation ActiveRelation */
$relation = $this->primaryModel->$relationName(); $relation = $this->primaryModel->$relationName();
$relation->primaryModel = null; $relation->primaryModel = null;
$this->via = array($relationName, $relation); $this->via = array($relationName, $relation);
if (is_array($options)) { if ($callback !== null) {
foreach ($options as $name => $value) { call_user_func($callback, $relation);
$this->$name = $value;
}
} elseif ($options instanceof \Closure) {
$options($relation);
} }
return $this; return $this;
} }
@ -71,10 +67,10 @@ class ActiveRelation extends ActiveQuery
/** /**
* @param string $tableName * @param string $tableName
* @param array $link * @param array $link
* @param array|\Closure $options * @param callback $callback
* @return ActiveRelation * @return ActiveRelation
*/ */
public function viaTable($tableName, $link, $options = null) public function viaTable($tableName, $link, $callback = null)
{ {
$relation = new ActiveRelation(array( $relation = new ActiveRelation(array(
'modelClass' => get_class($this->primaryModel), 'modelClass' => get_class($this->primaryModel),
@ -84,12 +80,8 @@ class ActiveRelation extends ActiveQuery
'asArray' => true, 'asArray' => true,
)); ));
$this->via = $relation; $this->via = $relation;
if (is_array($options)) { if ($callback !== null) {
foreach ($options as $name => $value) { call_user_func($callback, $relation);
$this->$name = $value;
}
} elseif ($options instanceof \Closure) {
$options($relation);
} }
return $this; return $this;
} }
@ -110,12 +102,14 @@ class ActiveRelation extends ActiveQuery
$this->filterByModels($viaModels); $this->filterByModels($viaModels);
} elseif (is_array($this->via)) { } elseif (is_array($this->via)) {
// via relation // via relation
$relationName = $this->via[0]; /** @var $viaQuery ActiveRelation */
$viaModels = $this->primaryModel->$relationName; list($viaName, $viaQuery) = $this->via;
if ($viaModels === null) { $viaQuery->primaryModel = $this->primaryModel;
$viaModels = array(); if ($viaQuery->multiple) {
} elseif (!is_array($viaModels)) { $this->primaryModel->$viaName = $viaModels = $viaQuery->all();
$viaModels = array($viaModels); } else {
$this->primaryModel->$viaName = $model = $viaQuery->one();
$viaModels = $model === null ? array() : array($model);
} }
$this->filterByModels($viaModels); $this->filterByModels($viaModels);
} else { } else {

Loading…
Cancel
Save