Files
yii2/tests/data/ar/Customer.php
Alexander Reviakin 7d8507dea8 Fix eager loading of nested one-to-many relations (#19585)
* Update ActiveRelationTrait.php

fix #19507

* update structure-applications.md wiki links (#19597)

* update structure-models.md wiki links (#19598)

* Update structure-views.md wiki links (#19600)

* update structure-filters.md links (#19601)

* Added nested via relations

* Added nested via relations eager loading test

* Update framework/db/ActiveRelationTrait.php

Co-authored-by: Bizley <pawel@positive.codes>

* Update tests/data/ar/Customer.php

Co-authored-by: Bizley <pawel@positive.codes>

* Update tests/data/ar/Customer.php

Co-authored-by: Bizley <pawel@positive.codes>

* Update tests/framework/db/ActiveRecordTest.php

Co-authored-by: Bizley <pawel@positive.codes>

* Update tests/framework/db/ActiveRecordTest.php

Co-authored-by: Bizley <pawel@positive.codes>

* Update tests/framework/db/ActiveRecordTest.php

Co-authored-by: Bizley <pawel@positive.codes>

* Update tests/framework/db/ActiveRecordTest.php

Co-authored-by: Bizley <pawel@positive.codes>

* Update tests/framework/db/ActiveRecordTest.php

Co-authored-by: Bizley <pawel@positive.codes>

* Update tests/framework/db/ActiveRecordTest.php

Co-authored-by: Bizley <pawel@positive.codes>

* Update tests/framework/db/ActiveRecordTest.php

Co-authored-by: Bizley <pawel@positive.codes>

* Fix #19507: Fix eager loading of nested relations

Co-authored-by: Ihor Sychevskyi <arhell333@gmail.com>
Co-authored-by: Bizley <pawel@positive.codes>
2022-10-04 15:55:53 +06:00

118 lines
3.0 KiB
PHP

<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yiiunit\data\ar;
use yii\db\ActiveQuery;
use yiiunit\framework\db\ActiveRecordTest;
/**
* Class Customer.
*
* @property int $id
* @property string $name
* @property string $email
* @property string $address
* @property int $status
*
* @method CustomerQuery findBySql($sql, $params = []) static
*/
class Customer extends ActiveRecord
{
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 2;
public $status2;
public $sumTotal;
public static function tableName()
{
return 'customer';
}
public function getProfile()
{
return $this->hasOne(Profile::className(), ['id' => 'profile_id']);
}
public function getOrdersPlain()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('[[id]]');
}
public function getExpensiveOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id');
}
public function getOrdersWithItems()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->with('orderItems');
}
public function getExpensiveOrdersWithNullFK()
{
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id');
}
public function getOrdersWithNullFK()
{
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->orderBy('id');
}
public function getOrders2()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id');
}
// deeply nested table relation
public function getOrderItems()
{
/* @var $rel ActiveQuery */
$rel = $this->hasMany(Item::className(), ['id' => 'item_id']);
return $rel->viaTable('order_item', ['order_id' => 'id'], function ($q) {
/* @var $q ActiveQuery */
$q->viaTable('order', ['customer_id' => 'id']);
})->orderBy('id');
}
public function getOrderItems2()
{
return $this->hasMany(OrderItem::className(), ['order_id' => 'id'])
->via('orders');
}
public function getItems()
{
return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems2');
}
public function afterSave($insert, $changedAttributes)
{
ActiveRecordTest::$afterSaveInsert = $insert;
ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
parent::afterSave($insert, $changedAttributes);
}
/**
* {@inheritdoc}
* @return CustomerQuery
*/
public static function find()
{
return new CustomerQuery(\get_called_class());
}
}