Compare commits

...

3 Commits

Author SHA1 Message Date
Carsten Brandt ea2cab8876 adjusted ActiveRelationTrait phpdoc 8 years ago
Carsten Brandt 6df20a0e7e adjusted test data to make existing relation tests fail 8 years ago
Ezekiel Fernandez 4c49f0f456 test for eager loading when there are 3 tables between the relation using via() to link it 8 years ago
  1. 8
      framework/db/ActiveRelationTrait.php
  2. 2
      tests/data/ar/Category.php
  3. 15
      tests/data/ar/Customer.php
  4. 54
      tests/data/cubrid.sql
  5. 54
      tests/data/mssql.sql
  6. 60
      tests/data/mysql.sql
  7. 54
      tests/data/oci.sql
  8. 54
      tests/data/postgres.sql
  9. 54
      tests/data/sqlite.sql
  10. 137
      tests/framework/ar/ActiveRecordTestTrait.php
  11. 204
      tests/framework/db/ActiveRecordTest.php

8
framework/db/ActiveRelationTrait.php

@ -56,7 +56,7 @@ trait ActiveRelationTrait
* is the "orders", and the inverse of the "orders" relation is the "customer". * 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. * 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, * 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. * This property is only used in relational context.
* @see inverseOf() * @see inverseOf()
*/ */
@ -139,8 +139,8 @@ trait ActiveRelationTrait
/** /**
* Finds the related records for the specified primary record. * 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. * This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
* @param string $name the relation name * @param string $name the relation name.
* @param ActiveRecordInterface|BaseActiveRecord $model the primary model * @param ActiveRecordInterface|BaseActiveRecord $model the primary model.
* @return mixed the related record(s) * @return mixed the related record(s)
* @throws InvalidParamException if the relation is invalid * @throws InvalidParamException if the relation is invalid
*/ */
@ -441,6 +441,7 @@ trait ActiveRelationTrait
} }
/** /**
* Adjust this query's condition to match related models primary keys only, i.e. to limit query to related records only.
* @param array $models * @param array $models
*/ */
private function filterByModels($models) private function filterByModels($models)
@ -456,6 +457,7 @@ trait ActiveRelationTrait
foreach ($models as $model) { foreach ($models as $model) {
if (($value = $model[$attribute]) !== null) { if (($value = $model[$attribute]) !== null) {
if (is_array($value)) { if (is_array($value)) {
// relation via multi value column
$values = array_merge($values, $value); $values = array_merge($values, $value);
} else { } else {
$values[] = $value; $values[] = $value;

2
tests/data/ar/Category.php

@ -28,7 +28,7 @@ class Category extends ActiveRecord
public function getLimitedItems() public function getLimitedItems()
{ {
return $this->hasMany(Item::className(), ['category_id' => 'id']) return $this->hasMany(Item::className(), ['category_id' => 'id'])
->onCondition(['item.id' => [1, 2, 3]]); ->onCondition(['item.id' => [21, 22, 23]]);
} }
public function getOrderItems() public function getOrderItems()

15
tests/data/ar/Customer.php

@ -44,6 +44,21 @@ class Customer extends ActiveRecord
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id'); 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() public function getExpensiveOrders()
{ {
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id'); return $this->hasMany(Order::className(), ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id');

54
tests/data/cubrid.sql

@ -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 ('Books');
INSERT INTO "category" (name) VALUES ('Movies'); 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" (id, name, category_id) VALUES (21, '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" (id, name, category_id) VALUES (22, 'Yii 1.1 Application Development Cookbook', 1);
INSERT INTO "item" (name, category_id) VALUES ('Ice Age', 2); INSERT INTO "item" (id, name, category_id) VALUES (23, 'Ice Age', 2);
INSERT INTO "item" (name, category_id) VALUES ('Toy Story', 2); INSERT INTO "item" (id, name, category_id) VALUES (24, 'Toy Story', 2);
INSERT INTO "item" (name, category_id) VALUES ('Cars', 2); INSERT INTO "item" (id, name, category_id) VALUES (25, 'Cars', 2);
INSERT INTO "order" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0); INSERT INTO "order" (id, customer_id, created_at, total) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0); INSERT INTO "order" (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0); INSERT INTO "order" (id, customer_id, created_at, total) VALUES (13, 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" (id, customer_id, created_at, total) VALUES (11, 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" (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325502201, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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); INSERT INTO "document" (title, content, version) VALUES ('Yii 2.0 guide', 'This is Yii 2.0 guide', 0);

54
tests/data/mssql.sql

@ -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 ('Books');
INSERT INTO [dbo].[category] ([name]) VALUES ('Movies'); 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] ([id], [name], [category_id]) VALUES (21, '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] ([id], [name], [category_id]) VALUES (22, 'Yii 1.1 Application Development Cookbook', 1);
INSERT INTO [dbo].[item] ([name], [category_id]) VALUES ('Ice Age', 2); INSERT INTO [dbo].[item] ([id], [name], [category_id]) VALUES (23, 'Ice Age', 2);
INSERT INTO [dbo].[item] ([name], [category_id]) VALUES ('Toy Story', 2); INSERT INTO [dbo].[item] ([id], [name], [category_id]) VALUES (24, 'Toy Story', 2);
INSERT INTO [dbo].[item] ([name], [category_id]) VALUES ('Cars', 2); INSERT INTO [dbo].[item] ([id], [name], [category_id]) VALUES (25, 'Cars', 2);
INSERT INTO [dbo].[order] ([customer_id], [created_at], [total]) VALUES (1, 1325282384, 110.0); INSERT INTO [dbo].[order] ([id], [customer_id], [created_at], [total]) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO [dbo].[order] ([customer_id], [created_at], [total]) VALUES (2, 1325334482, 33.0); INSERT INTO [dbo].[order] ([id], [customer_id], [created_at], [total]) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO [dbo].[order] ([customer_id], [created_at], [total]) VALUES (2, 1325502201, 40.0); INSERT INTO [dbo].[order] ([id], [customer_id], [created_at], [total]) VALUES (13, 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] ([id], [customer_id], [created_at], [total]) VALUES (11, 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] ([id], [customer_id], [created_at], [total]) VALUES (12, 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_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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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); INSERT INTO [dbo].[document] ([title], [content], [version]) VALUES ('Yii 2.0 guide', 'This is Yii 2.0 guide', 0);

60
tests/data/mysql.sql

@ -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 1');
INSERT INTO `profile` (description) VALUES ('profile customer 3'); INSERT INTO `profile` (description) VALUES ('profile customer 3');
INSERT INTO `customer` (email, name, address, status, profile_id) VALUES ('user1@example.com', 'user1', 'address1', 1, 1); INSERT INTO `customer` (id, email, name, address, status, profile_id) VALUES (1, 'user1@example.com', 'user1', 'address1', 1, 1);
INSERT INTO `customer` (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1); INSERT INTO `customer` (id, email, name, address, status) VALUES (2, 'user2@example.com', 'user2', 'address2', 1);
INSERT INTO `customer` (email, name, address, status, profile_id) VALUES ('user3@example.com', 'user3', 'address3', 2, 2); INSERT INTO `customer` (id, email, name, address, status, profile_id) VALUES (3, 'user3@example.com', 'user3', 'address3', 2, 2);
INSERT INTO `category` (name) VALUES ('Books'); INSERT INTO `category` (name) VALUES ('Books');
INSERT INTO `category` (name) VALUES ('Movies'); 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` (id, name, category_id) VALUES (21, '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` (id, name, category_id) VALUES (22, 'Yii 1.1 Application Development Cookbook', 1);
INSERT INTO `item` (name, category_id) VALUES ('Ice Age', 2); INSERT INTO `item` (id, name, category_id) VALUES (23, 'Ice Age', 2);
INSERT INTO `item` (name, category_id) VALUES ('Toy Story', 2); INSERT INTO `item` (id, name, category_id) VALUES (24, 'Toy Story', 2);
INSERT INTO `item` (name, category_id) VALUES ('Cars', 2); INSERT INTO `item` (id, name, category_id) VALUES (25, 'Cars', 2);
INSERT INTO `order` (customer_id, created_at, total) VALUES (1, 1325282384, 110.0); INSERT INTO `order` (id, customer_id, created_at, total) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325334482, 33.0); INSERT INTO `order` (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325502201, 40.0); INSERT INTO `order` (id, customer_id, created_at, total) VALUES (13, 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` (id, customer_id, created_at, total) VALUES (11, 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` (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO `order_with_null_fk` (customer_id, created_at, total) VALUES (2, 1325502201, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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); INSERT INTO `document` (title, content, version) VALUES ('Yii 2.0 guide', 'This is Yii 2.0 guide', 0);

54
tests/data/oci.sql

@ -263,33 +263,33 @@ INSERT INTO "customer" ("email", "name", "address", "status", "bool_status", "pr
INSERT INTO "category" ("name") VALUES ('Books'); INSERT INTO "category" ("name") VALUES ('Books');
INSERT INTO "category" ("name") VALUES ('Movies'); 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" ("id", "name", "category_id") VALUES (21, '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" ("id", "name", "category_id") VALUES (22, 'Yii 1.1 Application Development Cookbook', 1);
INSERT INTO "item" ("name", "category_id") VALUES ('Ice Age', 2); INSERT INTO "item" ("id", "name", "category_id") VALUES (23, 'Ice Age', 2);
INSERT INTO "item" ("name", "category_id") VALUES ('Toy Story', 2); INSERT INTO "item" ("id", "name", "category_id") VALUES (24, 'Toy Story', 2);
INSERT INTO "item" ("name", "category_id") VALUES ('Cars', 2); INSERT INTO "item" ("id", "name", "category_id") VALUES (25, 'Cars', 2);
INSERT INTO "order" ("customer_id", "created_at", "total") VALUES (1, 1325282384, 110.0); INSERT INTO "order" ("id", "customer_id", "created_at", "total") VALUES (11, 1, 1325282384, 110.0);
INSERT INTO "order" ("customer_id", "created_at", "total") VALUES (2, 1325334482, 33.0); INSERT INTO "order" ("id", "customer_id", "created_at", "total") VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order" ("customer_id", "created_at", "total") VALUES (2, 1325502201, 40.0); INSERT INTO "order" ("id", "customer_id", "created_at", "total") VALUES (13, 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" ("id", "customer_id", "created_at", "total") VALUES (11, 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" ("id", "customer_id", "created_at", "total") VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order_with_null_fk" ("customer_id", "created_at", "total") VALUES (2, 1325502201, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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); INSERT INTO "document" ("title", "content", "version") VALUES ('Yii 2.0 guide', 'This is Yii 2.0 guide', 0);

54
tests/data/postgres.sql

@ -181,33 +181,33 @@ INSERT INTO "customer" (email, name, address, status, bool_status, profile_id) V
INSERT INTO "category" (name) VALUES ('Books'); INSERT INTO "category" (name) VALUES ('Books');
INSERT INTO "category" (name) VALUES ('Movies'); 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" (id, name, category_id) VALUES (21, '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" (id, name, category_id) VALUES (22, 'Yii 1.1 Application Development Cookbook', 1);
INSERT INTO "item" (name, category_id) VALUES ('Ice Age', 2); INSERT INTO "item" (id, name, category_id) VALUES (23, 'Ice Age', 2);
INSERT INTO "item" (name, category_id) VALUES ('Toy Story', 2); INSERT INTO "item" (id, name, category_id) VALUES (24, 'Toy Story', 2);
INSERT INTO "item" (name, category_id) VALUES ('Cars', 2); INSERT INTO "item" (id, name, category_id) VALUES (25, 'Cars', 2);
INSERT INTO "order" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0); INSERT INTO "order" (id, customer_id, created_at, total) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0); INSERT INTO "order" (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0); INSERT INTO "order" (id, customer_id, created_at, total) VALUES (13, 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" (id, customer_id, created_at, total) VALUES (11, 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" (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325502201, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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); INSERT INTO "document" (title, content, version) VALUES ('Yii 2.0 guide', 'This is Yii 2.0 guide', 0);

54
tests/data/sqlite.sql

@ -147,33 +147,33 @@ INSERT INTO "customer" (email, name, address, status, profile_id) VALUES ('user3
INSERT INTO "category" (name) VALUES ('Books'); INSERT INTO "category" (name) VALUES ('Books');
INSERT INTO "category" (name) VALUES ('Movies'); 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" (id, name, category_id) VALUES (21, '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" (id, name, category_id) VALUES (22, 'Yii 1.1 Application Development Cookbook', 1);
INSERT INTO "item" (name, category_id) VALUES ('Ice Age', 2); INSERT INTO "item" (id, name, category_id) VALUES (23, 'Ice Age', 2);
INSERT INTO "item" (name, category_id) VALUES ('Toy Story', 2); INSERT INTO "item" (id, name, category_id) VALUES (24, 'Toy Story', 2);
INSERT INTO "item" (name, category_id) VALUES ('Cars', 2); INSERT INTO "item" (id, name, category_id) VALUES (25, 'Cars', 2);
INSERT INTO "order" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0); INSERT INTO "order" (id, customer_id, created_at, total) VALUES (11, 1, 1325282384, 110.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0); INSERT INTO "order" (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0); INSERT INTO "order" (id, customer_id, created_at, total) VALUES (13, 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" (id, customer_id, created_at, total) VALUES (11, 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" (id, customer_id, created_at, total) VALUES (12, 2, 1325334482, 33.0);
INSERT INTO "order_with_null_fk" (customer_id, created_at, total) VALUES (2, 1325502201, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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 (1, 1, 1, 30.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 (1, 2, 2, 40.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 (2, 4, 1, 10.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 (2, 5, 1, 15.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 (2, 3, 1, 8.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 (3, 2, 1, 40.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); INSERT INTO "document" (title, content, version) VALUES ('Yii 2.0 guide', 'This is Yii 2.0 guide', 0);

137
tests/framework/ar/ActiveRecordTestTrait.php

@ -275,7 +275,7 @@ trait ActiveRecordTestTrait
$this->assertTrue($customerA->equals($customerB)); $this->assertTrue($customerA->equals($customerB));
$customerA = $customerClass::findOne(1); $customerA = $customerClass::findOne(1);
$customerB = $itemClass::findOne(1); $customerB = $itemClass::findOne(21);
$this->assertFalse($customerA->equals($customerB)); $this->assertFalse($customerA->equals($customerB));
} }
@ -415,12 +415,12 @@ trait ActiveRecordTestTrait
/* @var $customer Customer */ /* @var $customer Customer */
$customer = $customerClass::findOne(2); $customer = $customerClass::findOne(2);
$this->assertFalse($customer->isRelationPopulated('orders')); $this->assertFalse($customer->isRelationPopulated('orders'));
$orders = $customer->getOrders()->where(['id' => 3])->all(); $orders = $customer->getOrders()->where(['id' => 13])->all();
$this->assertFalse($customer->isRelationPopulated('orders')); $this->assertFalse($customer->isRelationPopulated('orders'));
$this->assertEquals(0, count($customer->relatedRecords)); $this->assertEquals(0, count($customer->relatedRecords));
$this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
$this->assertEquals(3, $orders[0]->id); $this->assertEquals(13, $orders[0]->id);
} }
public function testFindEager() public function testFindEager()
@ -467,11 +467,11 @@ trait ActiveRecordTestTrait
/* @var $this TestCase|ActiveRecordTestTrait */ /* @var $this TestCase|ActiveRecordTestTrait */
/* @var $order Order */ /* @var $order Order */
$order = $orderClass::findOne(1); $order = $orderClass::findOne(11);
$this->assertEquals(1, $order->id); $this->assertEquals(11, $order->id);
$this->assertEquals(2, count($order->items)); $this->assertEquals(2, count($order->items));
$this->assertEquals(1, $order->items[0]->id); $this->assertEquals(21, $order->items[0]->id);
$this->assertEquals(2, $order->items[1]->id); $this->assertEquals(22, $order->items[1]->id);
} }
public function testFindLazyVia2() public function testFindLazyVia2()
@ -481,7 +481,7 @@ trait ActiveRecordTestTrait
/* @var $this TestCase|ActiveRecordTestTrait */ /* @var $this TestCase|ActiveRecordTestTrait */
/* @var $order Order */ /* @var $order Order */
$order = $orderClass::findOne(1); $order = $orderClass::findOne(11);
$order->id = 100; $order->id = 100;
$this->assertEquals([], $order->items); $this->assertEquals([], $order->items);
} }
@ -495,11 +495,11 @@ trait ActiveRecordTestTrait
$orders = $orderClass::find()->with('items')->orderBy('id')->all(); $orders = $orderClass::find()->with('items')->orderBy('id')->all();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$order = $orders[0]; $order = $orders[0];
$this->assertEquals(1, $order->id); $this->assertEquals(11, $order->id);
$this->assertTrue($order->isRelationPopulated('items')); $this->assertTrue($order->isRelationPopulated('items'));
$this->assertEquals(2, count($order->items)); $this->assertEquals(2, count($order->items));
$this->assertEquals(1, $order->items[0]->id); $this->assertEquals(21, $order->items[0]->id);
$this->assertEquals(2, $order->items[1]->id); $this->assertEquals(22, $order->items[1]->id);
} }
public function testFindNestedRelation() public function testFindNestedRelation()
@ -579,25 +579,25 @@ trait ActiveRecordTestTrait
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$order = $orders[0]; $order = $orders[0];
$this->assertEquals(1, $order->id); $this->assertEquals(11, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder1')); $this->assertTrue($order->isRelationPopulated('itemsInOrder1'));
$this->assertEquals(2, count($order->itemsInOrder1)); $this->assertEquals(2, count($order->itemsInOrder1));
$this->assertEquals(1, $order->itemsInOrder1[0]->id); $this->assertEquals(21, $order->itemsInOrder1[0]->id);
$this->assertEquals(2, $order->itemsInOrder1[1]->id); $this->assertEquals(22, $order->itemsInOrder1[1]->id);
$order = $orders[1]; $order = $orders[1];
$this->assertEquals(2, $order->id); $this->assertEquals(12, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder1')); $this->assertTrue($order->isRelationPopulated('itemsInOrder1'));
$this->assertEquals(3, count($order->itemsInOrder1)); $this->assertEquals(3, count($order->itemsInOrder1));
$this->assertEquals(5, $order->itemsInOrder1[0]->id); $this->assertEquals(25, $order->itemsInOrder1[0]->id);
$this->assertEquals(3, $order->itemsInOrder1[1]->id); $this->assertEquals(23, $order->itemsInOrder1[1]->id);
$this->assertEquals(4, $order->itemsInOrder1[2]->id); $this->assertEquals(24, $order->itemsInOrder1[2]->id);
$order = $orders[2]; $order = $orders[2];
$this->assertEquals(3, $order->id); $this->assertEquals(13, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder1')); $this->assertTrue($order->isRelationPopulated('itemsInOrder1'));
$this->assertEquals(1, count($order->itemsInOrder1)); $this->assertEquals(1, count($order->itemsInOrder1));
$this->assertEquals(2, $order->itemsInOrder1[0]->id); $this->assertEquals(22, $order->itemsInOrder1[0]->id);
} }
// different order in via table // different order in via table
@ -610,25 +610,25 @@ trait ActiveRecordTestTrait
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$order = $orders[0]; $order = $orders[0];
$this->assertEquals(1, $order->id); $this->assertEquals(11, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder2')); $this->assertTrue($order->isRelationPopulated('itemsInOrder2'));
$this->assertEquals(2, count($order->itemsInOrder2)); $this->assertEquals(2, count($order->itemsInOrder2));
$this->assertEquals(1, $order->itemsInOrder2[0]->id); $this->assertEquals(21, $order->itemsInOrder2[0]->id);
$this->assertEquals(2, $order->itemsInOrder2[1]->id); $this->assertEquals(22, $order->itemsInOrder2[1]->id);
$order = $orders[1]; $order = $orders[1];
$this->assertEquals(2, $order->id); $this->assertEquals(12, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder2')); $this->assertTrue($order->isRelationPopulated('itemsInOrder2'));
$this->assertEquals(3, count($order->itemsInOrder2)); $this->assertEquals(3, count($order->itemsInOrder2));
$this->assertEquals(5, $order->itemsInOrder2[0]->id); $this->assertEquals(25, $order->itemsInOrder2[0]->id);
$this->assertEquals(3, $order->itemsInOrder2[1]->id); $this->assertEquals(23, $order->itemsInOrder2[1]->id);
$this->assertEquals(4, $order->itemsInOrder2[2]->id); $this->assertEquals(24, $order->itemsInOrder2[2]->id);
$order = $orders[2]; $order = $orders[2];
$this->assertEquals(3, $order->id); $this->assertEquals(13, $order->id);
$this->assertTrue($order->isRelationPopulated('itemsInOrder2')); $this->assertTrue($order->isRelationPopulated('itemsInOrder2'));
$this->assertEquals(1, count($order->itemsInOrder2)); $this->assertEquals(1, count($order->itemsInOrder2));
$this->assertEquals(2, $order->itemsInOrder2[0]->id); $this->assertEquals(22, $order->itemsInOrder2[0]->id);
} }
public function testLink() public function testLink()
@ -668,17 +668,17 @@ trait ActiveRecordTestTrait
$this->assertEquals(1, $order->customer->primaryKey); $this->assertEquals(1, $order->customer->primaryKey);
// via model // via model
$order = $orderClass::findOne(1); $order = $orderClass::findOne(11);
$this->assertEquals(2, count($order->items)); $this->assertEquals(2, count($order->items));
$this->assertEquals(2, count($order->orderItems)); $this->assertEquals(2, count($order->orderItems));
$orderItem = $orderItemClass::findOne(['order_id' => 1, 'item_id' => 3]); $orderItem = $orderItemClass::findOne(['order_id' => 1, 'item_id' => 3]);
$this->assertNull($orderItem); $this->assertNull($orderItem);
$item = $itemClass::findOne(3); $item = $itemClass::findOne(23);
$order->link('items', $item, ['quantity' => 10, 'subtotal' => 100]); $order->link('items', $item, ['quantity' => 10, 'subtotal' => 100]);
$this->afterSave(); $this->afterSave();
$this->assertEquals(3, count($order->items)); $this->assertEquals(3, count($order->items));
$this->assertEquals(3, count($order->orderItems)); $this->assertEquals(3, count($order->orderItems));
$orderItem = $orderItemClass::findOne(['order_id' => 1, 'item_id' => 3]); $orderItem = $orderItemClass::findOne(['order_id' => 11, 'item_id' => 23]);
$this->assertTrue($orderItem instanceof $orderItemClass); $this->assertTrue($orderItem instanceof $orderItemClass);
$this->assertEquals(10, $orderItem->quantity); $this->assertEquals(10, $orderItem->quantity);
$this->assertEquals(100, $orderItem->subtotal); $this->assertEquals(100, $orderItem->subtotal);
@ -704,9 +704,9 @@ trait ActiveRecordTestTrait
$customer->unlink('ordersWithNullFK', $customer->ordersWithNullFK[1], false); $customer->unlink('ordersWithNullFK', $customer->ordersWithNullFK[1], false);
$this->assertEquals(1, count($customer->ordersWithNullFK)); $this->assertEquals(1, count($customer->ordersWithNullFK));
$orderWithNullFK = $orderWithNullFKClass::findOne(3); $orderWithNullFK = $orderWithNullFKClass::findOne(13);
$this->assertEquals(3,$orderWithNullFK->id); $this->assertEquals(13,$orderWithNullFK->id);
$this->assertNull($orderWithNullFK->customer_id); $this->assertNull($orderWithNullFK->customer_id);
// has many with delete // has many with delete
@ -716,10 +716,10 @@ trait ActiveRecordTestTrait
$this->afterSave(); $this->afterSave();
$this->assertEquals(1, count($customer->orders)); $this->assertEquals(1, count($customer->orders));
$this->assertNull($orderClass::findOne(3)); $this->assertNull($orderClass::findOne(13));
// via model with delete // via model with delete
$order = $orderClass::findOne(2); $order = $orderClass::findOne(12);
$this->assertEquals(3, count($order->items)); $this->assertEquals(3, count($order->items));
$this->assertEquals(3, count($order->orderItems)); $this->assertEquals(3, count($order->orderItems));
$order->unlink('items', $order->items[2], true); $order->unlink('items', $order->items[2], true);
@ -762,8 +762,8 @@ trait ActiveRecordTestTrait
$this->assertEquals(1, $orderClass::find()->count()); $this->assertEquals(1, $orderClass::find()->count());
$this->assertEquals(0, count($customer->orders)); $this->assertEquals(0, count($customer->orders));
$this->assertNull($orderClass::findOne(2)); $this->assertNull($orderClass::findOne(12));
$this->assertNull($orderClass::findOne(3)); $this->assertNull($orderClass::findOne(13));
// has many without delete // has many without delete
@ -774,12 +774,12 @@ trait ActiveRecordTestTrait
$this->afterSave(); $this->afterSave();
$this->assertEquals(0, count($customer->ordersWithNullFK)); $this->assertEquals(0, count($customer->ordersWithNullFK));
$this->assertEquals(3, $orderWithNullFKClass::find()->count()); $this->assertEquals(3, $orderWithNullFKClass::find()->count());
$this->assertEquals(2, $orderWithNullFKClass::find()->where(['AND', ['id' => [2, 3]], ['customer_id' => null]])->count()); $this->assertEquals(2, $orderWithNullFKClass::find()->where(['AND', ['id' => [12, 13]], ['customer_id' => null]])->count());
// via model with delete // via model with delete
/* @var $order Order */ /* @var $order Order */
$order = $orderClass::findOne(1); $order = $orderClass::findOne(11);
$this->assertEquals(2, count($order->books)); $this->assertEquals(2, count($order->books));
$orderItemCount = $orderItemClass::find()->count(); $orderItemCount = $orderItemClass::find()->count();
$this->assertEquals(5, $itemClass::find()->count()); $this->assertEquals(5, $itemClass::find()->count());
@ -796,7 +796,7 @@ trait ActiveRecordTestTrait
$order->unlinkAll('booksWithNullFK',false); $order->unlinkAll('booksWithNullFK',false);
$this->afterSave(); $this->afterSave();
$this->assertEquals(0, count($order->booksWithNullFK)); $this->assertEquals(0, count($order->booksWithNullFK));
$this->assertEquals(2, $orderItemsWithNullFKClass::find()->where(['AND', ['item_id' => [1, 2]], ['order_id' => null]])->count()); $this->assertEquals(2, $orderItemsWithNullFKClass::find()->where(['AND', ['item_id' => [21, 22]], ['order_id' => null]])->count());
$this->assertEquals($orderItemCount, $orderItemsWithNullFKClass::find()->count()); $this->assertEquals($orderItemCount, $orderItemsWithNullFKClass::find()->count());
$this->assertEquals(5, $itemClass::find()->count()); $this->assertEquals(5, $itemClass::find()->count());
@ -985,7 +985,7 @@ trait ActiveRecordTestTrait
$orderItemClass = $this->getOrderItemClass(); $orderItemClass = $this->getOrderItemClass();
/* @var $this TestCase|ActiveRecordTestTrait */ /* @var $this TestCase|ActiveRecordTestTrait */
// updateCounters // updateCounters
$pk = ['order_id' => 2, 'item_id' => 4]; $pk = ['order_id' => 12, 'item_id' => 24];
$orderItem = $orderItemClass::findOne($pk); $orderItem = $orderItemClass::findOne($pk);
$this->assertEquals(1, $orderItem->quantity); $this->assertEquals(1, $orderItem->quantity);
$ret = $orderItem->updateCounters(['quantity' => -1]); $ret = $orderItem->updateCounters(['quantity' => -1]);
@ -996,7 +996,7 @@ trait ActiveRecordTestTrait
$this->assertEquals(0, $orderItem->quantity); $this->assertEquals(0, $orderItem->quantity);
// updateAllCounters // updateAllCounters
$pk = ['order_id' => 1, 'item_id' => 2]; $pk = ['order_id' => 11, 'item_id' => 22];
$orderItem = $orderItemClass::findOne($pk); $orderItem = $orderItemClass::findOne($pk);
$this->assertEquals(2, $orderItem->quantity); $this->assertEquals(2, $orderItem->quantity);
$ret = $orderItemClass::updateAllCounters([ $ret = $orderItemClass::updateAllCounters([
@ -1102,7 +1102,7 @@ trait ActiveRecordTestTrait
$customer = $customerClass::find()->where(['id' => 1])->with('orders')->all(); $customer = $customerClass::find()->where(['id' => 1])->with('orders')->all();
$this->assertNotNull($customer); $this->assertNotNull($customer);
$this->assertEquals([ $this->assertEquals([
[$this->getOrderClass(), false, 1, false], [$this->getOrderClass(), false, 11, false],
[$customerClass, false, 1, true], [$customerClass, false, 1, true],
], $afterFindCalls); ], $afterFindCalls);
$afterFindCalls = []; $afterFindCalls = [];
@ -1115,9 +1115,9 @@ trait ActiveRecordTestTrait
} }
$this->assertNotNull($customer); $this->assertNotNull($customer);
$this->assertEquals([ $this->assertEquals([
[$orderClass, false, 1, false], [$orderClass, false, 11, false],
[$orderClass, false, 2, false], [$orderClass, false, 12, false],
[$orderClass, false, 3, false], [$orderClass, false, 13, false],
[$customerClass, false, 1, true], [$customerClass, false, 1, true],
[$customerClass, false, 2, true], [$customerClass, false, 2, true],
], $afterFindCalls); ], $afterFindCalls);
@ -1174,20 +1174,49 @@ trait ActiveRecordTestTrait
$orderClass = $this->getOrderClass(); $orderClass = $this->getOrderClass();
/* @var $order Order */ /* @var $order Order */
$order = $orderClass::find()->with('itemsIndexed')->where(['id' => 1])->one(); $order = $orderClass::find()->with('itemsIndexed')->where(['id' => 11])->one();
$this->assertTrue($order->isRelationPopulated('itemsIndexed')); $this->assertTrue($order->isRelationPopulated('itemsIndexed'));
$items = $order->itemsIndexed; $items = $order->itemsIndexed;
$this->assertEquals(2, count($items)); $this->assertEquals(2, count($items));
$this->assertTrue(isset($items[1])); $this->assertTrue(isset($items[21]));
$this->assertTrue(isset($items[2])); $this->assertTrue(isset($items[22]));
/* @var $order Order */ /* @var $order Order */
$order = $orderClass::find()->with('itemsIndexed')->where(['id' => 2])->one(); $order = $orderClass::find()->with('itemsIndexed')->where(['id' => 12])->one();
$this->assertTrue($order->isRelationPopulated('itemsIndexed')); $this->assertTrue($order->isRelationPopulated('itemsIndexed'));
$items = $order->itemsIndexed; $items = $order->itemsIndexed;
$this->assertEquals(3, count($items)); $this->assertEquals(3, count($items));
$this->assertTrue(isset($items[3])); $this->assertTrue(isset($items[23]));
$this->assertTrue(isset($items[4])); $this->assertTrue(isset($items[24]));
$this->assertTrue(isset($items[5])); $this->assertTrue(isset($items[25]));
}
public function testEagerLoadingWithViaRelation()
{
/* @var $this TestCase|ActiveRecordTestTrait */
/* @var $customerClass \yii\db\ActiveRecordInterface */
$customerClass = $this->getCustomerClass();
// two level deep
// customer -> orders -> order_items
/** @var $customer \yii\db\ActiveRecord $customer */
$customer = $customerClass::find()->where(['id' => 1])->with(['orderItems2'])->one();
$this->assertTrue($customer->isRelationPopulated('orderItems2'));
// three level deep
// customer -> orders -> order_items -> items
/** @var $customer \yii\db\ActiveRecord $customer */
$customer = $customerClass::find()->where(['id' => 1])->with(['items'])->one();
$this->assertTrue($customer->isRelationPopulated('items'));
// four level deep
// customer -> orders -> order_items -> items -> categories
/** @var $customer \yii\db\ActiveRecord $customer */
$customer = $customerClass::find()->where(['id' => 1])->with(['categories'])->one();
$this->assertTrue($customer->isRelationPopulated('categories'));
} }
} }

204
tests/framework/db/ActiveRecordTest.php

@ -142,17 +142,17 @@ abstract class ActiveRecordTest extends DatabaseTestCase
public function testFindLazyViaTable() public function testFindLazyViaTable()
{ {
/* @var $order Order */ /* @var $order Order */
$order = Order::findOne(1); $order = Order::findOne(11);
$this->assertEquals(1, $order->id); $this->assertEquals(11, $order->id);
$this->assertEquals(2, count($order->books)); $this->assertEquals(2, count($order->books));
$this->assertEquals(1, $order->items[0]->id); $this->assertEquals(21, $order->items[0]->id);
$this->assertEquals(2, $order->items[1]->id); $this->assertEquals(22, $order->items[1]->id);
$order = Order::findOne(2); $order = Order::findOne(12);
$this->assertEquals(2, $order->id); $this->assertEquals(12, $order->id);
$this->assertEquals(0, count($order->books)); $this->assertEquals(0, count($order->books));
$order = Order::find()->where(['id' => 1])->asArray()->one(); $order = Order::find()->where(['id' => 11])->asArray()->one();
$this->assertTrue(is_array($order)); $this->assertTrue(is_array($order));
} }
@ -162,19 +162,19 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$order = $orders[0]; $order = $orders[0];
$this->assertEquals(1, $order->id); $this->assertEquals(11, $order->id);
$this->assertEquals(2, count($order->books)); $this->assertEquals(2, count($order->books));
$this->assertEquals(1, $order->books[0]->id); $this->assertEquals(21, $order->books[0]->id);
$this->assertEquals(2, $order->books[1]->id); $this->assertEquals(22, $order->books[1]->id);
$order = $orders[1]; $order = $orders[1];
$this->assertEquals(2, $order->id); $this->assertEquals(12, $order->id);
$this->assertEquals(0, count($order->books)); $this->assertEquals(0, count($order->books));
$order = $orders[2]; $order = $orders[2];
$this->assertEquals(3, $order->id); $this->assertEquals(13, $order->id);
$this->assertEquals(1, count($order->books)); $this->assertEquals(1, count($order->books));
$this->assertEquals(2, $order->books[0]->id); $this->assertEquals(22, $order->books[0]->id);
// https://github.com/yiisoft/yii2/issues/1402 // https://github.com/yiisoft/yii2/issues/1402
$orders = Order::find()->with('books')->orderBy('id')->asArray()->all(); $orders = Order::find()->with('books')->orderBy('id')->asArray()->all();
@ -183,10 +183,10 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$order = $orders[0]; $order = $orders[0];
$this->assertTrue(is_array($order)); $this->assertTrue(is_array($order));
$this->assertEquals(1, $order['id']); $this->assertEquals(11, $order['id']);
$this->assertEquals(2, count($order['books'])); $this->assertEquals(2, count($order['books']));
$this->assertEquals(1, $order['books'][0]['id']); $this->assertEquals(21, $order['books'][0]['id']);
$this->assertEquals(2, $order['books'][1]['id']); $this->assertEquals(22, $order['books'][1]['id']);
} }
// deeply nested table relation // deeply nested table relation
@ -201,8 +201,8 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(2, count($items)); $this->assertEquals(2, count($items));
$this->assertInstanceOf(Item::className(), $items[0]); $this->assertInstanceOf(Item::className(), $items[0]);
$this->assertInstanceOf(Item::className(), $items[1]); $this->assertInstanceOf(Item::className(), $items[1]);
$this->assertEquals(1, $items[0]->id); $this->assertEquals(21, $items[0]->id);
$this->assertEquals(2, $items[1]->id); $this->assertEquals(22, $items[1]->id);
} }
/** /**
@ -222,14 +222,14 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$this->assertInstanceOf(Order::className(), $orders[1]); $this->assertInstanceOf(Order::className(), $orders[1]);
$ids = [$orders[0]->id, $orders[1]->id]; $ids = [$orders[0]->id, $orders[1]->id];
sort($ids); sort($ids);
$this->assertEquals([1, 3], $ids); $this->assertEquals([11, 13], $ids);
$category = Category::findOne(2); $category = Category::findOne(2);
$this->assertNotNull($category); $this->assertNotNull($category);
$orders = $category->orders; $orders = $category->orders;
$this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
$this->assertInstanceOf(Order::className(), $orders[0]); $this->assertInstanceOf(Order::className(), $orders[0]);
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
} }
@ -321,9 +321,9 @@ abstract class ActiveRecordTest extends DatabaseTestCase
// left join and eager loading // left join and eager loading
$orders = Order::find()->joinWith('customer')->orderBy('customer.id DESC, order.id')->all(); $orders = Order::find()->joinWith('customer')->orderBy('customer.id DESC, order.id')->all();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertEquals(1, $orders[2]->id); $this->assertEquals(11, $orders[2]->id);
$this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('customer'));
$this->assertTrue($orders[1]->isRelationPopulated('customer')); $this->assertTrue($orders[1]->isRelationPopulated('customer'));
$this->assertTrue($orders[2]->isRelationPopulated('customer')); $this->assertTrue($orders[2]->isRelationPopulated('customer'));
@ -335,8 +335,8 @@ abstract class ActiveRecordTest extends DatabaseTestCase
}, },
])->orderBy('order.id')->all(); ])->orderBy('order.id')->all();
$this->assertEquals(2, count($orders)); $this->assertEquals(2, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('customer'));
$this->assertTrue($orders[1]->isRelationPopulated('customer')); $this->assertTrue($orders[1]->isRelationPopulated('customer'));
@ -345,9 +345,9 @@ abstract class ActiveRecordTest extends DatabaseTestCase
'customer' => function ($query) { 'customer' => function ($query) {
$query->where(['customer.id' => 2]); $query->where(['customer.id' => 2]);
}, },
])->where(['order.id' => [1, 2]])->orderBy('order.id')->all(); ])->where(['order.id' => [11, 12]])->orderBy('order.id')->all();
$this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('customer'));
// inner join filtering without eager loading // inner join filtering without eager loading
@ -357,8 +357,8 @@ abstract class ActiveRecordTest extends DatabaseTestCase
}, },
], false)->orderBy('order.id')->all(); ], false)->orderBy('order.id')->all();
$this->assertEquals(2, count($orders)); $this->assertEquals(2, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertFalse($orders[0]->isRelationPopulated('customer')); $this->assertFalse($orders[0]->isRelationPopulated('customer'));
$this->assertFalse($orders[1]->isRelationPopulated('customer')); $this->assertFalse($orders[1]->isRelationPopulated('customer'));
@ -367,16 +367,16 @@ abstract class ActiveRecordTest extends DatabaseTestCase
'customer' => function ($query) { 'customer' => function ($query) {
$query->where(['customer.id' => 2]); $query->where(['customer.id' => 2]);
}, },
], false)->where(['order.id' => [1, 2]])->orderBy('order.id')->all(); ], false)->where(['order.id' => [11, 12]])->orderBy('order.id')->all();
$this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertFalse($orders[0]->isRelationPopulated('customer')); $this->assertFalse($orders[0]->isRelationPopulated('customer'));
// join with via-relation // join with via-relation
$orders = Order::find()->innerJoinWith('books')->orderBy('order.id')->all(); $orders = Order::find()->innerJoinWith('books')->orderBy('order.id')->all();
$this->assertEquals(2, count($orders)); $this->assertEquals(2, count($orders));
$this->assertEquals(1, $orders[0]->id); $this->assertEquals(11, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertTrue($orders[0]->isRelationPopulated('books')); $this->assertTrue($orders[0]->isRelationPopulated('books'));
$this->assertTrue($orders[1]->isRelationPopulated('books')); $this->assertTrue($orders[1]->isRelationPopulated('books'));
$this->assertEquals(2, count($orders[0]->books)); $this->assertEquals(2, count($orders[0]->books));
@ -393,7 +393,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
])->orderBy('order.id')->all(); ])->orderBy('order.id')->all();
$this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
$this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertTrue($orders[0]->isRelationPopulated('items'));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, count($orders[0]->items)); $this->assertEquals(3, count($orders[0]->items));
$this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category'));
$this->assertEquals(2, $orders[0]->items[0]->category->id); $this->assertEquals(2, $orders[0]->items[0]->category->id);
@ -405,9 +405,9 @@ abstract class ActiveRecordTest extends DatabaseTestCase
} }
])->orderBy('c.id DESC, order.id')->all(); ])->orderBy('c.id DESC, order.id')->all();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertEquals(1, $orders[2]->id); $this->assertEquals(11, $orders[2]->id);
$this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('customer'));
$this->assertTrue($orders[1]->isRelationPopulated('customer')); $this->assertTrue($orders[1]->isRelationPopulated('customer'));
$this->assertTrue($orders[2]->isRelationPopulated('customer')); $this->assertTrue($orders[2]->isRelationPopulated('customer'));
@ -415,9 +415,9 @@ abstract class ActiveRecordTest extends DatabaseTestCase
// join with table alias // join with table alias
$orders = Order::find()->joinWith('customer as c')->orderBy('c.id DESC, order.id')->all(); $orders = Order::find()->joinWith('customer as c')->orderBy('c.id DESC, order.id')->all();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertEquals(1, $orders[2]->id); $this->assertEquals(11, $orders[2]->id);
$this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('customer'));
$this->assertTrue($orders[1]->isRelationPopulated('customer')); $this->assertTrue($orders[1]->isRelationPopulated('customer'));
$this->assertTrue($orders[2]->isRelationPopulated('customer')); $this->assertTrue($orders[2]->isRelationPopulated('customer'));
@ -433,7 +433,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
])->orderBy('order.id')->all(); ])->orderBy('order.id')->all();
$this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
$this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertTrue($orders[0]->isRelationPopulated('items'));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, count($orders[0]->items)); $this->assertEquals(3, count($orders[0]->items));
$this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category'));
$this->assertEquals(2, $orders[0]->items[0]->category->id); $this->assertEquals(2, $orders[0]->items[0]->category->id);
@ -441,9 +441,9 @@ abstract class ActiveRecordTest extends DatabaseTestCase
// join with ON condition // join with ON condition
$orders = Order::find()->joinWith('books2')->orderBy('order.id')->all(); $orders = Order::find()->joinWith('books2')->orderBy('order.id')->all();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertEquals(1, $orders[0]->id); $this->assertEquals(11, $orders[0]->id);
$this->assertEquals(2, $orders[1]->id); $this->assertEquals(12, $orders[1]->id);
$this->assertEquals(3, $orders[2]->id); $this->assertEquals(13, $orders[2]->id);
$this->assertTrue($orders[0]->isRelationPopulated('books2')); $this->assertTrue($orders[0]->isRelationPopulated('books2'));
$this->assertTrue($orders[1]->isRelationPopulated('books2')); $this->assertTrue($orders[1]->isRelationPopulated('books2'));
$this->assertTrue($orders[2]->isRelationPopulated('books2')); $this->assertTrue($orders[2]->isRelationPopulated('books2'));
@ -452,19 +452,19 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(1, count($orders[2]->books2)); $this->assertEquals(1, count($orders[2]->books2));
// lazy loading with ON condition // lazy loading with ON condition
$order = Order::findOne(1); $order = Order::findOne(11);
$this->assertEquals(2, count($order->books2)); $this->assertEquals(2, count($order->books2));
$order = Order::findOne(2); $order = Order::findOne(12);
$this->assertEquals(0, count($order->books2)); $this->assertEquals(0, count($order->books2));
$order = Order::findOne(3); $order = Order::findOne(13);
$this->assertEquals(1, count($order->books2)); $this->assertEquals(1, count($order->books2));
// eager loading with ON condition // eager loading with ON condition
$orders = Order::find()->with('books2')->all(); $orders = Order::find()->with('books2')->all();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertEquals(1, $orders[0]->id); $this->assertEquals(11, $orders[0]->id);
$this->assertEquals(2, $orders[1]->id); $this->assertEquals(12, $orders[1]->id);
$this->assertEquals(3, $orders[2]->id); $this->assertEquals(13, $orders[2]->id);
$this->assertTrue($orders[0]->isRelationPopulated('books2')); $this->assertTrue($orders[0]->isRelationPopulated('books2'));
$this->assertTrue($orders[1]->isRelationPopulated('books2')); $this->assertTrue($orders[1]->isRelationPopulated('books2'));
$this->assertTrue($orders[2]->isRelationPopulated('books2')); $this->assertTrue($orders[2]->isRelationPopulated('books2'));
@ -480,7 +480,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
// https://github.com/yiisoft/yii2/issues/2880 // https://github.com/yiisoft/yii2/issues/2880
$query = Order::findOne(1); $query = Order::findOne(11);
$customer = $query->getCustomer()->joinWith([ $customer = $query->getCustomer()->joinWith([
'orders' => function ($q) { $q->orderBy([]); } 'orders' => function ($q) { $q->orderBy([]); }
])->one(); ])->one();
@ -505,7 +505,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
])->orderBy('order.id')->all(); ])->orderBy('order.id')->all();
$this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
$this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertTrue($orders[0]->isRelationPopulated('items'));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, count($orders[0]->items)); $this->assertEquals(3, count($orders[0]->items));
$this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category'));
$this->assertEquals(2, $orders[0]->items[0]->category->id); $this->assertEquals(2, $orders[0]->items[0]->category->id);
@ -584,9 +584,9 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$orders = $query->orderBy($query->applyAlias('customer', 'id') . ' DESC,' . $query->applyAlias('order', 'id'))->all(); $orders = $query->orderBy($query->applyAlias('customer', 'id') . ' DESC,' . $query->applyAlias('order', 'id'))->all();
} }
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertEquals(1, $orders[2]->id); $this->assertEquals(11, $orders[2]->id);
$this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('customer'));
$this->assertTrue($orders[1]->isRelationPopulated('customer')); $this->assertTrue($orders[1]->isRelationPopulated('customer'));
$this->assertTrue($orders[2]->isRelationPopulated('customer')); $this->assertTrue($orders[2]->isRelationPopulated('customer'));
@ -601,8 +601,8 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$orders = $query->where([$query->applyAlias('customer', 'id') => 2])->orderBy($query->applyAlias('order', 'id'))->all(); $orders = $query->where([$query->applyAlias('customer', 'id') => 2])->orderBy($query->applyAlias('order', 'id'))->all();
} }
$this->assertEquals(2, count($orders)); $this->assertEquals(2, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertTrue($orders[0]->isRelationPopulated('customer')); $this->assertTrue($orders[0]->isRelationPopulated('customer'));
$this->assertTrue($orders[1]->isRelationPopulated('customer')); $this->assertTrue($orders[1]->isRelationPopulated('customer'));
@ -616,8 +616,8 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$orders = $query->where([$query->applyAlias('customer', 'id') => 2])->orderBy($query->applyAlias('order', 'id'))->all(); $orders = $query->where([$query->applyAlias('customer', 'id') => 2])->orderBy($query->applyAlias('order', 'id'))->all();
} }
$this->assertEquals(2, count($orders)); $this->assertEquals(2, count($orders));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertFalse($orders[0]->isRelationPopulated('customer')); $this->assertFalse($orders[0]->isRelationPopulated('customer'));
$this->assertFalse($orders[1]->isRelationPopulated('customer')); $this->assertFalse($orders[1]->isRelationPopulated('customer'));
@ -631,8 +631,8 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$orders = $query->where([$query->applyAlias('book', 'name') => 'Yii 1.1 Application Development Cookbook'])->orderBy($query->applyAlias('order', 'id'))->all(); $orders = $query->where([$query->applyAlias('book', 'name') => 'Yii 1.1 Application Development Cookbook'])->orderBy($query->applyAlias('order', 'id'))->all();
} }
$this->assertEquals(2, count($orders)); $this->assertEquals(2, count($orders));
$this->assertEquals(1, $orders[0]->id); $this->assertEquals(11, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id); $this->assertEquals(13, $orders[1]->id);
$this->assertTrue($orders[0]->isRelationPopulated('books')); $this->assertTrue($orders[0]->isRelationPopulated('books'));
$this->assertTrue($orders[1]->isRelationPopulated('books')); $this->assertTrue($orders[1]->isRelationPopulated('books'));
$this->assertEquals(2, count($orders[0]->books)); $this->assertEquals(2, count($orders[0]->books));
@ -671,7 +671,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
} }
$this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
$this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertTrue($orders[0]->isRelationPopulated('items'));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, count($orders[0]->items)); $this->assertEquals(3, count($orders[0]->items));
$this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category'));
$this->assertEquals(2, $orders[0]->items[0]->category->id); $this->assertEquals(2, $orders[0]->items[0]->category->id);
@ -681,9 +681,9 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$relationName = 'books' . ucfirst($aliasMethod); $relationName = 'books' . ucfirst($aliasMethod);
$orders = Order::find()->joinWith(["$relationName b"])->orderBy('order.id')->all(); $orders = Order::find()->joinWith(["$relationName b"])->orderBy('order.id')->all();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertEquals(1, $orders[0]->id); $this->assertEquals(11, $orders[0]->id);
$this->assertEquals(2, $orders[1]->id); $this->assertEquals(12, $orders[1]->id);
$this->assertEquals(3, $orders[2]->id); $this->assertEquals(13, $orders[2]->id);
$this->assertTrue($orders[0]->isRelationPopulated($relationName)); $this->assertTrue($orders[0]->isRelationPopulated($relationName));
$this->assertTrue($orders[1]->isRelationPopulated($relationName)); $this->assertTrue($orders[1]->isRelationPopulated($relationName));
$this->assertTrue($orders[2]->isRelationPopulated($relationName)); $this->assertTrue($orders[2]->isRelationPopulated($relationName));
@ -697,9 +697,9 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$relationName = 'books' . ucfirst($aliasMethod) . 'A'; $relationName = 'books' . ucfirst($aliasMethod) . 'A';
$orders = Order::find()->joinWith(["$relationName"])->orderBy('order.id')->all(); $orders = Order::find()->joinWith(["$relationName"])->orderBy('order.id')->all();
$this->assertEquals(3, count($orders)); $this->assertEquals(3, count($orders));
$this->assertEquals(1, $orders[0]->id); $this->assertEquals(11, $orders[0]->id);
$this->assertEquals(2, $orders[1]->id); $this->assertEquals(12, $orders[1]->id);
$this->assertEquals(3, $orders[2]->id); $this->assertEquals(13, $orders[2]->id);
$this->assertTrue($orders[0]->isRelationPopulated($relationName)); $this->assertTrue($orders[0]->isRelationPopulated($relationName));
$this->assertTrue($orders[1]->isRelationPopulated($relationName)); $this->assertTrue($orders[1]->isRelationPopulated($relationName));
$this->assertTrue($orders[2]->isRelationPopulated($relationName)); $this->assertTrue($orders[2]->isRelationPopulated($relationName));
@ -724,14 +724,14 @@ abstract class ActiveRecordTest extends DatabaseTestCase
// relational query // relational query
/** @var $order Order */ /** @var $order Order */
$order = Order::findOne(1); $order = Order::findOne(11);
$customerQuery = $order->getCustomer()->innerJoinWith(['orders o'], false); $customerQuery = $order->getCustomer()->innerJoinWith(['orders o'], false);
if ($aliasMethod === 'explicit') { if ($aliasMethod === 'explicit') {
$customer = $customerQuery->where(['o.id' => 1])->one(); $customer = $customerQuery->where(['o.id' => 11])->one();
} elseif ($aliasMethod === 'querysyntax') { } elseif ($aliasMethod === 'querysyntax') {
$customer = $customerQuery->where(['{{@order}}.id' => 1])->one(); $customer = $customerQuery->where(['{{@order}}.id' => 11])->one();
} elseif ($aliasMethod === 'applyAlias') { } elseif ($aliasMethod === 'applyAlias') {
$customer = $customerQuery->where([$query->applyAlias('order', 'id') => 1])->one(); $customer = $customerQuery->where([$query->applyAlias('order', 'id') => 11])->one();
} }
$this->assertNotNull($customer); $this->assertNotNull($customer);
$this->assertEquals(1, $customer->id); $this->assertEquals(1, $customer->id);
@ -753,7 +753,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
])->orderBy('order.id')->all(); ])->orderBy('order.id')->all();
$this->assertEquals(1, count($orders)); $this->assertEquals(1, count($orders));
$this->assertTrue($orders[0]->isRelationPopulated('items')); $this->assertTrue($orders[0]->isRelationPopulated('items'));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertEquals(3, count($orders[0]->items)); $this->assertEquals(3, count($orders[0]->items));
$this->assertTrue($orders[0]->items[0]->isRelationPopulated('category')); $this->assertTrue($orders[0]->items[0]->isRelationPopulated('category'));
$this->assertEquals(2, $orders[0]->items[0]->category->id); $this->assertEquals(2, $orders[0]->items[0]->category->id);
@ -771,7 +771,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
->where(['movies.name' => 'Toy Story']); ->where(['movies.name' => 'Toy Story']);
$orders = $query->all(); $orders = $query->all();
$this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertFalse($orders[0]->isRelationPopulated('bookItems')); $this->assertFalse($orders[0]->isRelationPopulated('bookItems'));
$this->assertFalse($orders[0]->isRelationPopulated('movieItems')); $this->assertFalse($orders[0]->isRelationPopulated('movieItems'));
// with eager loading // with eager loading
@ -781,7 +781,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
->where(['movies.name' => 'Toy Story']); ->where(['movies.name' => 'Toy Story']);
$orders = $query->all(); $orders = $query->all();
$this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertTrue($orders[0]->isRelationPopulated('bookItems')); $this->assertTrue($orders[0]->isRelationPopulated('bookItems'));
$this->assertTrue($orders[0]->isRelationPopulated('movieItems')); $this->assertTrue($orders[0]->isRelationPopulated('movieItems'));
$this->assertEquals(0, count($orders[0]->bookItems)); $this->assertEquals(0, count($orders[0]->bookItems));
@ -796,7 +796,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
->where(['movies.name' => 'Toy Story']); ->where(['movies.name' => 'Toy Story']);
$orders = $query->all(); $orders = $query->all();
$this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertFalse($orders[0]->isRelationPopulated('itemsIndexed')); $this->assertFalse($orders[0]->isRelationPopulated('itemsIndexed'));
// with eager loading, only for one relation as it would be overwritten otherwise. // with eager loading, only for one relation as it would be overwritten otherwise.
$query = Order::find() $query = Order::find()
@ -805,7 +805,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
->where(['movies.name' => 'Toy Story']); ->where(['movies.name' => 'Toy Story']);
$orders = $query->all(); $orders = $query->all();
$this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertTrue($orders[0]->isRelationPopulated('itemsIndexed')); $this->assertTrue($orders[0]->isRelationPopulated('itemsIndexed'));
$this->assertEquals(3, count($orders[0]->itemsIndexed)); $this->assertEquals(3, count($orders[0]->itemsIndexed));
// with eager loading, and the other relation // with eager loading, and the other relation
@ -815,7 +815,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
->where(['movies.name' => 'Toy Story']); ->where(['movies.name' => 'Toy Story']);
$orders = $query->all(); $orders = $query->all();
$this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true)); $this->assertEquals(1, count($orders), $query->createCommand()->rawSql . print_r($orders, true));
$this->assertEquals(2, $orders[0]->id); $this->assertEquals(12, $orders[0]->id);
$this->assertTrue($orders[0]->isRelationPopulated('itemsIndexed')); $this->assertTrue($orders[0]->isRelationPopulated('itemsIndexed'));
$this->assertEquals(0, count($orders[0]->itemsIndexed)); $this->assertEquals(0, count($orders[0]->itemsIndexed));
} }
@ -827,7 +827,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
public function testFindCompositeRelationWithJoin() public function testFindCompositeRelationWithJoin()
{ {
/* @var $orderItem OrderItem */ /* @var $orderItem OrderItem */
$orderItem = OrderItem::findOne([1, 1]); $orderItem = OrderItem::findOne([11, 21]);
$orderItemNoJoin = $orderItem->orderItemCompositeNoJoin; $orderItemNoJoin = $orderItem->orderItemCompositeNoJoin;
$this->assertInstanceOf('yiiunit\data\ar\OrderItem', $orderItemNoJoin); $this->assertInstanceOf('yiiunit\data\ar\OrderItem', $orderItemNoJoin);
@ -839,7 +839,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
public function testFindSimpleRelationWithJoin() public function testFindSimpleRelationWithJoin()
{ {
/* @var $order Order */ /* @var $order Order */
$order = Order::findOne(1); $order = Order::findOne(11);
$customerNoJoin = $order->customer; $customerNoJoin = $order->customer;
$this->assertInstanceOf('yiiunit\data\ar\Customer', $customerNoJoin); $this->assertInstanceOf('yiiunit\data\ar\Customer', $customerNoJoin);
@ -874,14 +874,14 @@ abstract class ActiveRecordTest extends DatabaseTestCase
OrderItem::$tableName = $orderItemTableName; OrderItem::$tableName = $orderItemTableName;
/** @var $order Order */ /** @var $order Order */
$order = Order::findOne(1); $order = Order::findOne(11);
$itemsSQL = $order->getOrderitems()->createCommand()->rawSql; $itemsSQL = $order->getOrderitems()->createCommand()->rawSql;
$expectedSQL = $this->replaceQuotes("SELECT * FROM [[order_item]] WHERE [[order_id]]=1"); $expectedSQL = $this->replaceQuotes("SELECT * FROM [[order_item]] WHERE [[order_id]]=11");
$this->assertEquals($expectedSQL, $itemsSQL); $this->assertEquals($expectedSQL, $itemsSQL);
$order = Order::findOne(1); $order = Order::findOne(11);
$itemsSQL = $order->getOrderItems()->joinWith('item')->createCommand()->rawSql; $itemsSQL = $order->getOrderItems()->joinWith('item')->createCommand()->rawSql;
$expectedSQL = $this->replaceQuotes("SELECT [[order_item]].* FROM [[order_item]] LEFT JOIN [[item]] ON [[order_item]].[[item_id]] = [[item]].[[id]] WHERE [[order_item]].[[order_id]]=1"); $expectedSQL = $this->replaceQuotes("SELECT [[order_item]].* FROM [[order_item]] LEFT JOIN [[item]] ON [[order_item]].[[item_id]] = [[item]].[[id]] WHERE [[order_item]].[[order_id]]=11");
$this->assertEquals($expectedSQL, $itemsSQL); $this->assertEquals($expectedSQL, $itemsSQL);
Order::$tableName = null; Order::$tableName = null;
@ -939,25 +939,25 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customers[0]['id']); $this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customers[0]['id']);
$this->assertTrue(empty($customers[1]['orders2'])); $this->assertTrue(empty($customers[1]['orders2']));
$orders = Order::find()->with('customer2')->where(['id' => 1])->all(); $orders = Order::find()->with('customer2')->where(['id' => 11])->all();
$this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]); $this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]);
$order = Order::find()->with('customer2')->where(['id' => 1])->one(); $order = Order::find()->with('customer2')->where(['id' => 11])->one();
$this->assertTrue($order->customer2->orders2 === [$order]); $this->assertTrue($order->customer2->orders2 === [$order]);
$orders = Order::find()->with('customer2')->where(['id' => 1])->asArray()->all(); $orders = Order::find()->with('customer2')->where(['id' => 11])->asArray()->all();
$this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']); $this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
$order = Order::find()->with('customer2')->where(['id' => 1])->asArray()->one(); $order = Order::find()->with('customer2')->where(['id' => 11])->asArray()->one();
$this->assertTrue($order['customer2']['orders2'][0]['id'] === $orders[0]['id']); $this->assertTrue($order['customer2']['orders2'][0]['id'] === $orders[0]['id']);
$orders = Order::find()->with('customer2')->where(['id' => [1, 3]])->all(); $orders = Order::find()->with('customer2')->where(['id' => [11, 13]])->all();
$this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]); $this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]);
$this->assertTrue($orders[1]->customer2->orders2 === [$orders[1]]); $this->assertTrue($orders[1]->customer2->orders2 === [$orders[1]]);
$orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->all(); $orders = Order::find()->with('customer2')->where(['id' => [12, 13]])->orderBy('id')->all();
$this->assertTrue($orders[0]->customer2->orders2 === $orders); $this->assertTrue($orders[0]->customer2->orders2 === $orders);
$this->assertTrue($orders[1]->customer2->orders2 === $orders); $this->assertTrue($orders[1]->customer2->orders2 === $orders);
$orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->asArray()->all(); $orders = Order::find()->with('customer2')->where(['id' => [12, 13]])->orderBy('id')->asArray()->all();
$this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']); $this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
$this->assertTrue($orders[0]['customer2']['orders2'][1]['id'] === $orders[1]['id']); $this->assertTrue($orders[0]['customer2']['orders2'][1]['id'] === $orders[1]['id']);
$this->assertTrue($orders[1]['customer2']['orders2'][0]['id'] === $orders[0]['id']); $this->assertTrue($orders[1]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
@ -1033,7 +1033,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
// via table with delete // via table with delete
/* @var $order Order */ /* @var $order Order */
$order = $orderClass::findOne(1); $order = $orderClass::findOne(11);
$this->assertEquals(2, count($order->booksViaTable)); $this->assertEquals(2, count($order->booksViaTable));
$orderItemCount = $orderItemClass::find()->count(); $orderItemCount = $orderItemClass::find()->count();
$this->assertEquals(5, $itemClass::find()->count()); $this->assertEquals(5, $itemClass::find()->count());
@ -1049,7 +1049,7 @@ abstract class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(5, $itemClass::find()->count()); $this->assertEquals(5, $itemClass::find()->count());
$order->unlinkAll('booksWithNullFKViaTable',false); $order->unlinkAll('booksWithNullFKViaTable',false);
$this->assertEquals(0, count($order->booksWithNullFKViaTable)); $this->assertEquals(0, count($order->booksWithNullFKViaTable));
$this->assertEquals(2,$orderItemsWithNullFKClass::find()->where(['AND', ['item_id' => [1, 2]], ['order_id' => null]])->count()); $this->assertEquals(2,$orderItemsWithNullFKClass::find()->where(['AND', ['item_id' => [21, 22]], ['order_id' => null]])->count());
$this->assertEquals($orderItemCount, $orderItemsWithNullFKClass::find()->count()); $this->assertEquals($orderItemCount, $orderItemsWithNullFKClass::find()->count());
$this->assertEquals(5, $itemClass::find()->count()); $this->assertEquals(5, $itemClass::find()->count());
} }
@ -1198,15 +1198,15 @@ abstract class ActiveRecordTest extends DatabaseTestCase
->all(); ->all();
$expected = [ $expected = [
[ [
'order_id' => 1, 'order_id' => 11,
'subtotal' => 70, 'subtotal' => 70,
], ],
[ [
'order_id' => 2, 'order_id' => 12,
'subtotal' => 33, 'subtotal' => 33,
], ],
[ [
'order_id' => 3, 'order_id' => 13,
'subtotal' => 40, 'subtotal' => 40,
], ],
]; ];
@ -1248,31 +1248,31 @@ abstract class ActiveRecordTest extends DatabaseTestCase
{ {
$order = Order::find() $order = Order::find()
->with('orderItems2') ->with('orderItems2')
->where(['id' => 1]) ->where(['id' => 11])
->one(); ->one();
$orderItem = new OrderItem([ $orderItem = new OrderItem([
'order_id' => $order->id, 'order_id' => $order->id,
'item_id' => 3, 'item_id' => 23,
'quantity' => 1, 'quantity' => 1,
'subtotal' => 10.0, 'subtotal' => 10.0,
]); ]);
$order->link('orderItems2', $orderItem); $order->link('orderItems2', $orderItem);
$this->assertTrue(isset($order->orderItems2['3'])); $this->assertTrue(isset($order->orderItems2['23']));
} }
public function testLinkWhenRelationIsIndexed3() public function testLinkWhenRelationIsIndexed3()
{ {
$order = Order::find() $order = Order::find()
->with('orderItems3') ->with('orderItems3')
->where(['id' => 1]) ->where(['id' => 11])
->one(); ->one();
$orderItem = new OrderItem([ $orderItem = new OrderItem([
'order_id' => $order->id, 'order_id' => $order->id,
'item_id' => 3, 'item_id' => 23,
'quantity' => 1, 'quantity' => 1,
'subtotal' => 10.0, 'subtotal' => 10.0,
]); ]);
$order->link('orderItems3', $orderItem); $order->link('orderItems3', $orderItem);
$this->assertTrue(isset($order->orderItems3['1_3'])); $this->assertTrue(isset($order->orderItems3['11_23']));
} }
} }

Loading…
Cancel
Save