mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-23 15:37:24 +01:00
20160611
This commit is contained in:
@@ -34,7 +34,7 @@ trait ActiveQueryTrait
|
||||
/**
|
||||
* Sets the [[asArray]] property.
|
||||
* @param boolean $value whether to return the query results in terms of arrays instead of Active Records.
|
||||
* @return static the query object itself
|
||||
* @return $this the query object itself
|
||||
*/
|
||||
public function asArray($value = true)
|
||||
{
|
||||
@@ -53,35 +53,39 @@ trait ActiveQueryTrait
|
||||
* For example, `orders.address` means the `address` relation defined
|
||||
* in the model class corresponding to the `orders` relation.
|
||||
*
|
||||
<<<<<<< HEAD
|
||||
<<<<<<< HEAD
|
||||
* The followings are some usage examples:
|
||||
=======
|
||||
* The following are some usage examples:
|
||||
>>>>>>> yiichina/master
|
||||
=======
|
||||
* The following are some usage examples:
|
||||
>>>>>>> master
|
||||
*
|
||||
* ~~~
|
||||
* ```php
|
||||
* // find customers together with their orders and country
|
||||
* Customer::find()->with('orders', 'country')->all();
|
||||
* // find customers together with their orders and the orders' shipping address
|
||||
* Customer::find()->with('orders.address')->all();
|
||||
* // find customers together with their country and orders of status 1
|
||||
* Customer::find()->with([
|
||||
* 'orders' => function ($query) {
|
||||
* 'orders' => function (\yii\db\ActiveQuery $query) {
|
||||
* $query->andWhere('status = 1');
|
||||
* },
|
||||
* 'country',
|
||||
* ])->all();
|
||||
* ~~~
|
||||
* ```
|
||||
*
|
||||
* You can call `with()` multiple times. Each call will add relations to the existing ones.
|
||||
* For example, the following two statements are equivalent:
|
||||
*
|
||||
* ~~~
|
||||
* ```php
|
||||
* Customer::find()->with('orders', 'country')->all();
|
||||
* Customer::find()->with('orders')->with('country')->all();
|
||||
* ~~~
|
||||
* ```
|
||||
*
|
||||
* @return static the query object itself
|
||||
* @return $this the query object itself
|
||||
*/
|
||||
public function with()
|
||||
{
|
||||
@@ -95,7 +99,7 @@ trait ActiveQueryTrait
|
||||
$this->with = $with;
|
||||
} elseif (!empty($with)) {
|
||||
foreach ($with as $name => $value) {
|
||||
if (is_integer($name)) {
|
||||
if (is_int($name)) {
|
||||
// repeating relation is fine as normalizeRelations() handle it well
|
||||
$this->with[] = $value;
|
||||
} else {
|
||||
@@ -163,7 +167,10 @@ trait ActiveQueryTrait
|
||||
*/
|
||||
public function findWith($with, &$models)
|
||||
{
|
||||
$primaryModel = new $this->modelClass;
|
||||
$primaryModel = reset($models);
|
||||
if (!$primaryModel instanceof ActiveRecordInterface) {
|
||||
$primaryModel = new $this->modelClass;
|
||||
}
|
||||
$relations = $this->normalizeRelations($primaryModel, $with);
|
||||
/* @var $relation ActiveQuery */
|
||||
foreach ($relations as $name => $relation) {
|
||||
@@ -184,7 +191,7 @@ trait ActiveQueryTrait
|
||||
{
|
||||
$relations = [];
|
||||
foreach ($with as $name => $callback) {
|
||||
if (is_integer($name)) {
|
||||
if (is_int($name)) {
|
||||
$name = $callback;
|
||||
$callback = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user