|
|
@ -51,11 +51,14 @@ class ActiveRelation extends ActiveQuery |
|
|
|
* @param string $relationName |
|
|
|
* @param string $relationName |
|
|
|
* @param callback $callback |
|
|
|
* @param callback $callback |
|
|
|
* @return ActiveRelation |
|
|
|
* @return ActiveRelation |
|
|
|
|
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function via($relationName, $callback = null) |
|
|
|
public function via($relationName, $callback = null) |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** @var $relation ActiveRelation */ |
|
|
|
$getter = 'get' . $relationName; |
|
|
|
$relation = $this->primaryModel->$relationName(); |
|
|
|
if (method_exists($this->primaryModel, $getter)) { |
|
|
|
|
|
|
|
$relation = $this->primaryModel->$getter(); |
|
|
|
|
|
|
|
if ($relation instanceof ActiveRelation) { |
|
|
|
$relation->primaryModel = null; |
|
|
|
$relation->primaryModel = null; |
|
|
|
$this->via = array($relationName, $relation); |
|
|
|
$this->via = array($relationName, $relation); |
|
|
|
if ($callback !== null) { |
|
|
|
if ($callback !== null) { |
|
|
@ -63,6 +66,9 @@ class ActiveRelation extends ActiveQuery |
|
|
|
} |
|
|
|
} |
|
|
|
return $this; |
|
|
|
return $this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
throw new Exception('Unknown relation: ' . $relationName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param string $tableName |
|
|
|
* @param string $tableName |
|
|
@ -106,9 +112,11 @@ class ActiveRelation extends ActiveQuery |
|
|
|
list($viaName, $viaQuery) = $this->via; |
|
|
|
list($viaName, $viaQuery) = $this->via; |
|
|
|
$viaQuery->primaryModel = $this->primaryModel; |
|
|
|
$viaQuery->primaryModel = $this->primaryModel; |
|
|
|
if ($viaQuery->multiple) { |
|
|
|
if ($viaQuery->multiple) { |
|
|
|
$this->primaryModel->$viaName = $viaModels = $viaQuery->all(); |
|
|
|
$viaModels = $viaQuery->all(); |
|
|
|
|
|
|
|
$this->primaryModel->populateRelation($viaName, $viaModels); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$this->primaryModel->$viaName = $model = $viaQuery->one(); |
|
|
|
$model = $viaQuery->one(); |
|
|
|
|
|
|
|
$this->primaryModel->populateRelation($viaName, $model); |
|
|
|
$viaModels = $model === null ? array() : array($model); |
|
|
|
$viaModels = $model === null ? array() : array($model); |
|
|
|
} |
|
|
|
} |
|
|
|
$this->filterByModels($viaModels); |
|
|
|
$this->filterByModels($viaModels); |
|
|
@ -144,8 +152,12 @@ class ActiveRelation extends ActiveQuery |
|
|
|
if (count($primaryModels) === 1 && !$this->multiple) { |
|
|
|
if (count($primaryModels) === 1 && !$this->multiple) { |
|
|
|
$model = $this->one(); |
|
|
|
$model = $this->one(); |
|
|
|
foreach ($primaryModels as $i => $primaryModel) { |
|
|
|
foreach ($primaryModels as $i => $primaryModel) { |
|
|
|
|
|
|
|
if ($primaryModel instanceof ActiveRecord) { |
|
|
|
|
|
|
|
$primaryModel->populateRelation($name, $model); |
|
|
|
|
|
|
|
} else { |
|
|
|
$primaryModels[$i][$name] = $model; |
|
|
|
$primaryModels[$i][$name] = $model; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return array($model); |
|
|
|
return array($model); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$models = $this->all(); |
|
|
|
$models = $this->all(); |
|
|
@ -158,10 +170,11 @@ class ActiveRelation extends ActiveQuery |
|
|
|
$link = array_values(isset($viaQuery) ? $viaQuery->link : $this->link); |
|
|
|
$link = array_values(isset($viaQuery) ? $viaQuery->link : $this->link); |
|
|
|
foreach ($primaryModels as $i => $primaryModel) { |
|
|
|
foreach ($primaryModels as $i => $primaryModel) { |
|
|
|
$key = $this->getModelKey($primaryModel, $link); |
|
|
|
$key = $this->getModelKey($primaryModel, $link); |
|
|
|
if (isset($buckets[$key])) { |
|
|
|
$value = isset($buckets[$key]) ? $buckets[$key] : ($this->multiple ? array() : null); |
|
|
|
$primaryModels[$i][$name] = $buckets[$key]; |
|
|
|
if ($primaryModel instanceof ActiveRecord) { |
|
|
|
|
|
|
|
$primaryModel->populateRelation($name, $value); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$primaryModels[$i][$name] = $this->multiple ? array() : null; |
|
|
|
$primaryModels[$i][$name] = $value; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return $models; |
|
|
|
return $models; |
|
|
@ -262,11 +275,4 @@ class ActiveRelation extends ActiveQuery |
|
|
|
$sql = $db->getQueryBuilder()->build($this); |
|
|
|
$sql = $db->getQueryBuilder()->build($this); |
|
|
|
return $db->createCommand($sql, $this->params)->queryAll(); |
|
|
|
return $db->createCommand($sql, $this->params)->queryAll(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function link($model) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 1. Set models |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|