public function testActiveRelation() { /** @var Customer $customer */ $customer = Customer::findOne(2); $provider = new ActiveDataProvider(['query' => $customer->getOrders()]); $orders = $provider->getModels(); $this->assertEquals(2, count($orders)); $this->assertTrue($orders[0] instanceof Order); $this->assertTrue($orders[1] instanceof Order); $this->assertEquals([2, 3], $provider->getKeys()); $provider = new ActiveDataProvider(['query' => $customer->getOrders(), 'pagination' => ['pageSize' => 1]]); $orders = $provider->getModels(); $this->assertEquals(1, count($orders)); }
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 testMasterSlave() { $counts = [[0, 2], [1, 2], [2, 2]]; foreach ($counts as $count) { list($masterCount, $slaveCount) = $count; $db = $this->prepareMasterSlave($masterCount, $slaveCount); $this->assertTrue($db->getSlave() instanceof Connection); $this->assertTrue($db->getSlave()->isActive); $this->assertFalse($db->isActive); // test SELECT uses slave $this->assertEquals(2, $db->createCommand('SELECT COUNT(*) FROM profile')->queryScalar()); $this->assertFalse($db->isActive); // test UPDATE uses master $db->createCommand("UPDATE profile SET description='test' WHERE id=1")->execute(); $this->assertTrue($db->isActive); $this->assertNotEquals('test', $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar()); $result = $db->useMaster(function (Connection $db) { return $db->createCommand("SELECT description FROM profile WHERE id=1")->queryScalar(); }); $this->assertEquals('test', $result); // test ActiveRecord read/write split ActiveRecord::$db = $db = $this->prepareMasterSlave($masterCount, $slaveCount); $this->assertFalse($db->isActive); $customer = Customer::findOne(1); $this->assertTrue($customer instanceof Customer); $this->assertEquals('user1', $customer->name); $this->assertFalse($db->isActive); $customer->name = 'test'; $customer->save(); $this->assertTrue($db->isActive); $customer = Customer::findOne(1); $this->assertTrue($customer instanceof Customer); $this->assertEquals('user1', $customer->name); $result = $db->useMaster(function () { return Customer::findOne(1)->name; }); $this->assertEquals('test', $result); } }