Skip to content

Deeply nesting relation using via does not work #11898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions framework/db/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ trait ActiveRelationTrait
* is the "orders", and the inverse of the "orders" relation is the "customer".
* If this property is set, the primary record(s) will be referenced through the specified relation.
* For example, `$customer->orders[0]->customer` and `$customer` will be the same object,
* and accessing the customer of an order will not trigger new DB query.
* and accessing the customer of an order will not trigger a new DB query.
* This property is only used in relational context.
* @see inverseOf()
*/
Expand Down Expand Up @@ -139,8 +139,8 @@ public function inverseOf($relationName)
/**
* Finds the related records for the specified primary record.
* This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
* @param string $name the relation name
* @param ActiveRecordInterface|BaseActiveRecord $model the primary model
* @param string $name the relation name.
* @param ActiveRecordInterface|BaseActiveRecord $model the primary model.
* @return mixed the related record(s)
* @throws InvalidParamException if the relation is invalid
*/
Expand Down Expand Up @@ -441,6 +441,7 @@ private function prefixKeyColumns($attributes)
}

/**
* Adjust this query's condition to match related models primary keys only, i.e. to limit query to related records only.
* @param array $models
*/
private function filterByModels($models)
Expand All @@ -456,6 +457,7 @@ private function filterByModels($models)
foreach ($models as $model) {
if (($value = $model[$attribute]) !== null) {
if (is_array($value)) {
// relation via multi value column
$values = array_merge($values, $value);
} else {
$values[] = $value;
Expand Down
2 changes: 1 addition & 1 deletion tests/data/ar/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getItems()
public function getLimitedItems()
{
return $this->hasMany(Item::className(), ['category_id' => 'id'])
->onCondition(['item.id' => [1, 2, 3]]);
->onCondition(['item.id' => [21, 22, 23]]);
}

public function getOrderItems()
Expand Down
15 changes: 15 additions & 0 deletions tests/data/ar/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ public function getOrders()
return $this->hasMany(Order::className(), ['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 getCategories()
{
return $this->hasMany(Category::className(), ['id' => 'category_id'])->via('items');
}

public function getExpensiveOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id');
Expand Down
54 changes: 27 additions & 27 deletions tests/data/cubrid.sql
Original file line number Diff line number Diff line change
Expand Up @@ -163,33 +163,33 @@ INSERT INTO "customer" (email, name, address, status, profile_id) VALUES ('user3
INSERT INTO "category" (name) VALUES ('Books');
INSERT INTO "category" (name) VALUES ('Movies');

INSERT INTO "item" (name, category_id) VALUES ('Agile Web Application Development with Yii1.1 and PHP5', 1);
INSERT INTO "item" (name, category_id) VALUES ('Yii 1.1 Application Development Cookbook', 1);
INSERT INTO "item" (name, category_id) VALUES ('Ice Age', 2);
INSERT INTO "item" (name, category_id) VALUES ('Toy Story', 2);
INSERT INTO "item" (name, category_id) VALUES ('Cars', 2);

INSERT INTO "order" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);

INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);

INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);

INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
INSERT INTO "item" (id, name, category_id) VALUES (21, 'Agile Web Application Development with Yii1.1 and PHP5', 1);
INSERT INTO "item" (id, name, category_id) VALUES (22, 'Yii 1.1 Application Development Cookbook', 1);
INSERT INTO "item" (id, name, category_id) VALUES (23, 'Ice Age', 2);
INSERT INTO "item" (id, name, category_id) VALUES (24, 'Toy Story', 2);
INSERT INTO "item" (id, name, category_id) VALUES (25, 'Cars', 2);

INSERT INTO "order" (id, customer_id, created_at, total) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO "order" (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order" (id, customer_id, created_at, total) VALUES (13, 2, 1325502201, 40.0);

INSERT INTO "order_with_null_fk" (id, customer_id, created_at, total) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO "order_with_null_fk" (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order_with_null_fk" (id, customer_id, created_at, total) VALUES (13, 2, 1325502201, 40.0);

INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (11, 21, 1, 30.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (11, 22, 2, 40.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (12, 24, 1, 10.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (12, 25, 1, 15.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (12, 23, 1, 8.0);
INSERT INTO "order_item" (order_id, item_id, quantity, subtotal) VALUES (13, 22, 1, 40.0);

INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (11, 21, 1, 30.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (11, 22, 2, 40.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (12, 24, 1, 10.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (12, 25, 1, 15.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (12, 23, 1, 8.0);
INSERT INTO "order_item_with_null_fk" (order_id, item_id, quantity, subtotal) VALUES (13, 22, 1, 40.0);

INSERT INTO "document" (title, content, version) VALUES ('Yii 2.0 guide', 'This is Yii 2.0 guide', 0);

Expand Down
54 changes: 27 additions & 27 deletions tests/data/mssql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -153,33 +153,33 @@ INSERT INTO [dbo].[customer] ([email], [name], [address], [status], [profile_id]
INSERT INTO [dbo].[category] ([name]) VALUES ('Books');
INSERT INTO [dbo].[category] ([name]) VALUES ('Movies');

INSERT INTO [dbo].[item] ([name], [category_id]) VALUES ('Agile Web Application Development with Yii1.1 and PHP5', 1);
INSERT INTO [dbo].[item] ([name], [category_id]) VALUES ('Yii 1.1 Application Development Cookbook', 1);
INSERT INTO [dbo].[item] ([name], [category_id]) VALUES ('Ice Age', 2);
INSERT INTO [dbo].[item] ([name], [category_id]) VALUES ('Toy Story', 2);
INSERT INTO [dbo].[item] ([name], [category_id]) VALUES ('Cars', 2);

INSERT INTO [dbo].[order] ([customer_id], [created_at], [total]) VALUES (1, 1325282384, 110.0);
INSERT INTO [dbo].[order] ([customer_id], [created_at], [total]) VALUES (2, 1325334482, 33.0);
INSERT INTO [dbo].[order] ([customer_id], [created_at], [total]) VALUES (2, 1325502201, 40.0);

INSERT INTO [dbo].[order_with_null_fk] ([customer_id], [created_at], [total]) VALUES (1, 1325282384, 110.0);
INSERT INTO [dbo].[order_with_null_fk] ([customer_id], [created_at], [total]) VALUES (2, 1325334482, 33.0);
INSERT INTO [dbo].[order_with_null_fk] ([customer_id], [created_at], [total]) VALUES (2, 1325502201, 40.0);

INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (1, 1, 1, 30.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (1, 2, 2, 40.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 4, 1, 10.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 5, 1, 15.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 3, 1, 8.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (3, 2, 1, 40.0);

INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (1, 1, 1, 30.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (1, 2, 2, 40.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 4, 1, 10.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 5, 1, 15.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (2, 3, 1, 8.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (3, 2, 1, 40.0);
INSERT INTO [dbo].[item] ([id], [name], [category_id]) VALUES (21, 'Agile Web Application Development with Yii1.1 and PHP5', 1);
INSERT INTO [dbo].[item] ([id], [name], [category_id]) VALUES (22, 'Yii 1.1 Application Development Cookbook', 1);
INSERT INTO [dbo].[item] ([id], [name], [category_id]) VALUES (23, 'Ice Age', 2);
INSERT INTO [dbo].[item] ([id], [name], [category_id]) VALUES (24, 'Toy Story', 2);
INSERT INTO [dbo].[item] ([id], [name], [category_id]) VALUES (25, 'Cars', 2);

INSERT INTO [dbo].[order] ([id], [customer_id], [created_at], [total]) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO [dbo].[order] ([id], [customer_id], [created_at], [total]) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO [dbo].[order] ([id], [customer_id], [created_at], [total]) VALUES (13, 2, 1325502201, 40.0);

INSERT INTO [dbo].[order_with_null_fk] ([id], [customer_id], [created_at], [total]) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO [dbo].[order_with_null_fk] ([id], [customer_id], [created_at], [total]) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO [dbo].[order_with_null_fk] ([id], [customer_id], [created_at], [total]) VALUES (13, 2, 1325502201, 40.0);

INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (11, 21, 1, 30.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (11, 22, 2, 40.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (12, 24, 1, 10.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (12, 25, 1, 15.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (12, 23, 1, 8.0);
INSERT INTO [dbo].[order_item] ([order_id], [item_id], [quantity], [subtotal]) VALUES (13, 22, 1, 40.0);

INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (11, 21, 1, 30.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (11, 22, 2, 40.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (12, 24, 1, 10.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (12, 25, 1, 15.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (12, 23, 1, 8.0);
INSERT INTO [dbo].[order_item_with_null_fk] ([order_id], [item_id], [quantity], [subtotal]) VALUES (13, 22, 1, 40.0);

INSERT INTO [dbo].[document] ([title], [content], [version]) VALUES ('Yii 2.0 guide', 'This is Yii 2.0 guide', 0);

Expand Down
60 changes: 30 additions & 30 deletions tests/data/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -167,40 +167,40 @@ INSERT INTO `animal` (`type`) VALUES ('yiiunit\data\ar\Dog');
INSERT INTO `profile` (description) VALUES ('profile customer 1');
INSERT INTO `profile` (description) VALUES ('profile customer 3');

INSERT INTO `customer` (email, name, address, status, profile_id) VALUES ('[email protected]', 'user1', 'address1', 1, 1);
INSERT INTO `customer` (email, name, address, status) VALUES ('[email protected]', 'user2', 'address2', 1);
INSERT INTO `customer` (email, name, address, status, profile_id) VALUES ('[email protected]', 'user3', 'address3', 2, 2);
INSERT INTO `customer` (id, email, name, address, status, profile_id) VALUES (1, '[email protected]', 'user1', 'address1', 1, 1);
INSERT INTO `customer` (id, email, name, address, status) VALUES (2, '[email protected]', 'user2', 'address2', 1);
INSERT INTO `customer` (id, email, name, address, status, profile_id) VALUES (3, '[email protected]', 'user3', 'address3', 2, 2);

INSERT INTO `category` (name) VALUES ('Books');
INSERT INTO `category` (name) VALUES ('Movies');

INSERT INTO `item` (name, category_id) VALUES ('Agile Web Application Development with Yii1.1 and PHP5', 1);
INSERT INTO `item` (name, category_id) VALUES ('Yii 1.1 Application Development Cookbook', 1);
INSERT INTO `item` (name, category_id) VALUES ('Ice Age', 2);
INSERT INTO `item` (name, category_id) VALUES ('Toy Story', 2);
INSERT INTO `item` (name, category_id) VALUES ('Cars', 2);

INSERT INTO `order` (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);

INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (1, 1325282384, 110.0);
INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (2, 1325334482, 33.0);
INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);

INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);

INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (1, 1, 1, 30.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (1, 2, 2, 40.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (2, 4, 1, 10.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (2, 5, 1, 15.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (2, 3, 1, 8.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (3, 2, 1, 40.0);
INSERT INTO `item` (id, name, category_id) VALUES (21, 'Agile Web Application Development with Yii1.1 and PHP5', 1);
INSERT INTO `item` (id, name, category_id) VALUES (22, 'Yii 1.1 Application Development Cookbook', 1);
INSERT INTO `item` (id, name, category_id) VALUES (23, 'Ice Age', 2);
INSERT INTO `item` (id, name, category_id) VALUES (24, 'Toy Story', 2);
INSERT INTO `item` (id, name, category_id) VALUES (25, 'Cars', 2);

INSERT INTO `order` (id, customer_id, created_at, total) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO `order` (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO `order` (id, customer_id, created_at, total) VALUES (13, 2, 1325502201, 40.0);

INSERT INTO `order_with_null_fk` (id, customer_id, created_at, total) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO `order_with_null_fk` (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO `order_with_null_fk` (id, customer_id, created_at, total) VALUES (13, 2, 1325502201, 40.0);

INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (11, 21, 1, 30.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (11, 22, 2, 40.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (12, 24, 1, 10.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (12, 25, 1, 15.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (12, 23, 1, 8.0);
INSERT INTO `order_item` (order_id, item_id, quantity, subtotal) VALUES (13, 22, 1, 40.0);

INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (11, 21, 1, 30.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (11, 22, 2, 40.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (12, 24, 1, 10.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (12, 25, 1, 15.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (12, 23, 1, 8.0);
INSERT INTO `order_item_with_null_fk` (order_id, item_id, quantity, subtotal) VALUES (13, 22, 1, 40.0);

INSERT INTO `document` (title, content, version) VALUES ('Yii 2.0 guide', 'This is Yii 2.0 guide', 0);

Expand Down
Loading