public function testPopulateWithoutPk()
 {
     // tests with single pk asArray
     $aggregation = Customer::find()->select(['{{customer}}.[[status]]', 'SUM({{order}}.[[total]]) AS [[sumtotal]]'])->joinWith('ordersPlain', false)->groupBy('{{customer}}.[[status]]')->orderBy('status')->asArray()->all();
     $expected = [['status' => 1, 'sumtotal' => 183], ['status' => 2, 'sumtotal' => 0]];
     $this->assertEquals($expected, $aggregation);
     // tests with single pk with Models
     $aggregation = Customer::find()->select(['{{customer}}.[[status]]', 'SUM({{order}}.[[total]]) AS [[sumTotal]]'])->joinWith('ordersPlain', false)->groupBy('{{customer}}.[[status]]')->orderBy('status')->all();
     $this->assertCount(2, $aggregation);
     $this->assertContainsOnlyInstancesOf(Customer::className(), $aggregation);
     foreach ($aggregation as $item) {
         if ($item->status == 1) {
             $this->assertEquals(183, $item->sumTotal);
         } elseif ($item->status == 2) {
             $this->assertEquals(0, $item->sumTotal);
         }
     }
     // tests with composite pk asArray
     $aggregation = OrderItem::find()->select(['[[order_id]]', 'SUM([[subtotal]]) AS [[subtotal]]'])->joinWith('order', false)->groupBy('[[order_id]]')->orderBy('[[order_id]]')->asArray()->all();
     $expected = [['order_id' => 1, 'subtotal' => 70], ['order_id' => 2, 'subtotal' => 33], ['order_id' => 3, 'subtotal' => 40]];
     $this->assertEquals($expected, $aggregation);
     // tests with composite pk with Models
     $aggregation = OrderItem::find()->select(['[[order_id]]', 'SUM([[subtotal]]) AS [[subtotal]]'])->joinWith('order', false)->groupBy('[[order_id]]')->orderBy('[[order_id]]')->all();
     $this->assertCount(3, $aggregation);
     $this->assertContainsOnlyInstancesOf(OrderItem::className(), $aggregation);
     foreach ($aggregation as $item) {
         if ($item->order_id == 1) {
             $this->assertEquals(70, $item->subtotal);
         } elseif ($item->order_id == 2) {
             $this->assertEquals(33, $item->subtotal);
         } elseif ($item->order_id == 3) {
             $this->assertEquals(40, $item->subtotal);
         }
     }
 }
Example #2
0
 public function testInverseOf()
 {
     // eager loading: find one and all
     $customer = Customer::find()->with('orders2')->where(['id' => 1])->one();
     $this->assertTrue($customer->orders2[0]->customer2 === $customer);
     $customers = Customer::find()->with('orders2')->where(['id' => [1, 3]])->all();
     $this->assertTrue($customers[0]->orders2[0]->customer2 === $customers[0]);
     $this->assertTrue(empty($customers[1]->orders2));
     // lazy loading
     $customer = Customer::findOne(2);
     $orders = $customer->orders2;
     $this->assertTrue(count($orders) === 2);
     $this->assertTrue($customer->orders2[0]->customer2 === $customer);
     $this->assertTrue($customer->orders2[1]->customer2 === $customer);
     // ad-hoc lazy loading
     $customer = Customer::findOne(2);
     $orders = $customer->getOrders2()->all();
     $this->assertTrue(count($orders) === 2);
     $this->assertTrue($customer->orders2[0]->customer2 === $customer);
     $this->assertTrue($customer->orders2[1]->customer2 === $customer);
     // the other way around
     $customer = Customer::find()->with('orders2')->where(['id' => 1])->asArray()->one();
     $this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customer['id']);
     $customers = Customer::find()->with('orders2')->where(['id' => [1, 3]])->asArray()->all();
     $this->assertTrue($customer['orders2'][0]['customer2']['id'] === $customers[0]['id']);
     $this->assertTrue(empty($customers[1]['orders2']));
     $orders = Order::find()->with('customer2')->where(['id' => 1])->all();
     $this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]);
     $order = Order::find()->with('customer2')->where(['id' => 1])->one();
     $this->assertTrue($order->customer2->orders2 === [$order]);
     $orders = Order::find()->with('customer2')->where(['id' => 1])->asArray()->all();
     $this->assertTrue($orders[0]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
     $order = Order::find()->with('customer2')->where(['id' => 1])->asArray()->one();
     $this->assertTrue($order['customer2']['orders2'][0]['id'] === $orders[0]['id']);
     $orders = Order::find()->with('customer2')->where(['id' => [1, 3]])->all();
     $this->assertTrue($orders[0]->customer2->orders2 === [$orders[0]]);
     $this->assertTrue($orders[1]->customer2->orders2 === [$orders[1]]);
     $orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->all();
     $this->assertTrue($orders[0]->customer2->orders2 === $orders);
     $this->assertTrue($orders[1]->customer2->orders2 === $orders);
     $orders = Order::find()->with('customer2')->where(['id' => [2, 3]])->orderBy('id')->asArray()->all();
     $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[1]['customer2']['orders2'][0]['id'] === $orders[0]['id']);
     $this->assertTrue($orders[1]['customer2']['orders2'][1]['id'] === $orders[1]['id']);
 }
 public function testActiveQuery()
 {
     $db = $this->getConnection();
     $query = Customer::find()->orderBy('id');
     $customers = [];
     foreach ($query->batch(2, $db) as $models) {
         $customers = array_merge($customers, $models);
     }
     $this->assertEquals(3, count($customers));
     $this->assertEquals('user1', $customers[0]->name);
     $this->assertEquals('user2', $customers[1]->name);
     $this->assertEquals('user3', $customers[2]->name);
     // batch with eager loading
     $query = Customer::find()->with('orders')->orderBy('id');
     $customers = [];
     foreach ($query->batch(2, $db) as $models) {
         $customers = array_merge($customers, $models);
         foreach ($models as $model) {
             $this->assertTrue($model->isRelationPopulated('orders'));
         }
     }
     $this->assertEquals(3, count($customers));
     $this->assertEquals(1, count($customers[0]->orders));
     $this->assertEquals(2, count($customers[1]->orders));
     $this->assertEquals(0, count($customers[2]->orders));
 }