public function testArrayAttributeRelationUnLink() { /* @var $order Order */ $order = Order::find()->where(['id' => 1])->one(); $items = $order->itemsByArrayValue; $this->assertEquals(2, count($items)); $this->assertTrue(isset($items[1])); $this->assertTrue(isset($items[2])); $item = Item::get(2); $order->unlink('itemsByArrayValue', $item); $this->afterSave(); $items = $order->itemsByArrayValue; $this->assertEquals(1, count($items)); $this->assertTrue(isset($items[1])); $this->assertFalse(isset($items[2])); // check also after refresh $this->assertTrue($order->refresh()); $items = $order->itemsByArrayValue; $this->assertEquals(1, count($items)); $this->assertTrue(isset($items[1])); $this->assertFalse(isset($items[2])); }
public function getBooksWithNullFK() { return $this->hasMany(Item::className(), ['id' => 'item_id'])->via('orderItemsWithNullFK')->where(['category_id' => 1]); }
public function setUp() { parent::setUp(); /** @var Connection $db */ $db = ActiveRecord::$db = $this->getConnection(); // delete index if ($db->createCommand()->indexExists('yiitest')) { $db->createCommand()->deleteIndex('yiitest'); } $db->createCommand()->createIndex('yiitest'); $command = $db->createCommand(); Customer::setUpMapping($command); Item::setUpMapping($command); Order::setUpMapping($command); OrderItem::setUpMapping($command); OrderWithNullFK::setUpMapping($command); OrderItemWithNullFK::setUpMapping($command); $db->createCommand()->flushIndex('yiitest'); $customer = new Customer(); $customer->id = 1; $customer->setAttributes(['email' => '*****@*****.**', 'name' => 'user1', 'address' => 'address1', 'status' => 1], false); $customer->save(false); $customer = new Customer(); $customer->id = 2; $customer->setAttributes(['email' => '*****@*****.**', 'name' => 'user2', 'address' => 'address2', 'status' => 1], false); $customer->save(false); $customer = new Customer(); $customer->id = 3; $customer->setAttributes(['email' => '*****@*****.**', 'name' => 'user3', 'address' => 'address3', 'status' => 2], false); $customer->save(false); // INSERT INTO category (name) VALUES ('Books'); // INSERT INTO category (name) VALUES ('Movies'); $item = new Item(); $item->id = 1; $item->setAttributes(['name' => 'Agile Web Application Development with Yii1.1 and PHP5', 'category_id' => 1], false); $item->save(false); $item = new Item(); $item->id = 2; $item->setAttributes(['name' => 'Yii 1.1 Application Development Cookbook', 'category_id' => 1], false); $item->save(false); $item = new Item(); $item->id = 3; $item->setAttributes(['name' => 'Ice Age', 'category_id' => 2], false); $item->save(false); $item = new Item(); $item->id = 4; $item->setAttributes(['name' => 'Toy Story', 'category_id' => 2], false); $item->save(false); $item = new Item(); $item->id = 5; $item->setAttributes(['name' => 'Cars', 'category_id' => 2], false); $item->save(false); $order = new Order(); $order->id = 1; $order->setAttributes(['customer_id' => 1, 'created_at' => 1325282384, 'total' => 110.0], false); $order->save(false); $order = new Order(); $order->id = 2; $order->setAttributes(['customer_id' => 2, 'created_at' => 1325334482, 'total' => 33.0], false); $order->save(false); $order = new Order(); $order->id = 3; $order->setAttributes(['customer_id' => 2, 'created_at' => 1325502201, 'total' => 40.0], false); $order->save(false); $orderItem = new OrderItem(); $orderItem->setAttributes(['order_id' => 1, 'item_id' => 1, 'quantity' => 1, 'subtotal' => 30.0], false); $orderItem->save(false); $orderItem = new OrderItem(); $orderItem->setAttributes(['order_id' => 1, 'item_id' => 2, 'quantity' => 2, 'subtotal' => 40.0], false); $orderItem->save(false); $orderItem = new OrderItem(); $orderItem->setAttributes(['order_id' => 2, 'item_id' => 4, 'quantity' => 1, 'subtotal' => 10.0], false); $orderItem->save(false); $orderItem = new OrderItem(); $orderItem->setAttributes(['order_id' => 2, 'item_id' => 5, 'quantity' => 1, 'subtotal' => 15.0], false); $orderItem->save(false); $orderItem = new OrderItem(); $orderItem->setAttributes(['order_id' => 2, 'item_id' => 3, 'quantity' => 1, 'subtotal' => 8.0], false); $orderItem->save(false); $orderItem = new OrderItem(); $orderItem->setAttributes(['order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0], false); $orderItem->save(false); $order = new OrderWithNullFK(); $order->id = 1; $order->setAttributes(['customer_id' => 1, 'created_at' => 1325282384, 'total' => 110.0], false); $order->save(false); $order = new OrderWithNullFK(); $order->id = 2; $order->setAttributes(['customer_id' => 2, 'created_at' => 1325334482, 'total' => 33.0], false); $order->save(false); $order = new OrderWithNullFK(); $order->id = 3; $order->setAttributes(['customer_id' => 2, 'created_at' => 1325502201, 'total' => 40.0], false); $order->save(false); $orderItem = new OrderItemWithNullFK(); $orderItem->setAttributes(['order_id' => 1, 'item_id' => 1, 'quantity' => 1, 'subtotal' => 30.0], false); $orderItem->save(false); $orderItem = new OrderItemWithNullFK(); $orderItem->setAttributes(['order_id' => 1, 'item_id' => 2, 'quantity' => 2, 'subtotal' => 40.0], false); $orderItem->save(false); $orderItem = new OrderItemWithNullFK(); $orderItem->setAttributes(['order_id' => 2, 'item_id' => 4, 'quantity' => 1, 'subtotal' => 10.0], false); $orderItem->save(false); $orderItem = new OrderItemWithNullFK(); $orderItem->setAttributes(['order_id' => 2, 'item_id' => 5, 'quantity' => 1, 'subtotal' => 15.0], false); $orderItem->save(false); $orderItem = new OrderItemWithNullFK(); $orderItem->setAttributes(['order_id' => 2, 'item_id' => 3, 'quantity' => 1, 'subtotal' => 8.0], false); $orderItem->save(false); $orderItem = new OrderItemWithNullFK(); $orderItem->setAttributes(['order_id' => 3, 'item_id' => 2, 'quantity' => 1, 'subtotal' => 40.0], false); $orderItem->save(false); $db->createCommand()->flushIndex('yiitest'); }
public function getItemsInOrder2() { return $this->hasMany(Item::className(), ['id' => 'item_id'])->via('orderItems', function ($q) { $q->orderBy(['subtotal' => SORT_DESC]); })->orderBy('name'); }
public function getItem() { return $this->hasOne(Item::className(), ['id' => 'item_id']); }