Order.php 3.2 KB
Newer Older
Qiang Xue committed
1 2 3 4
<?php

namespace yiiunit\data\ar;

5 6 7 8 9
/**
 * Class Order
 *
 * @property integer $id
 * @property integer $customer_id
Alexander Kochetov committed
10
 * @property integer $created_at
11 12
 * @property string $total
 */
Qiang Xue committed
13 14
class Order extends ActiveRecord
{
15 16
    public static function tableName()
    {
17
        return 'order';
18
    }
Qiang Xue committed
19

20 21 22 23
    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }
Qiang Xue committed
24

25 26 27 28
    public function getCustomer2()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id'])->inverseOf('orders2');
    }
29

30 31 32 33
    public function getOrderItems()
    {
        return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
    }
Qiang Xue committed
34

Carsten Brandt committed
35 36 37 38 39
    public function getOrderItemsWithNullFK()
    {
        return $this->hasMany(OrderItemWithNullFK::className(), ['order_id' => 'id']);
    }

40 41 42 43 44
    public function getItems()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItems', function ($q) {
                // additional query configuration
Carsten Brandt committed
45
            })->orderBy('item.id');
46
    }
Qiang Xue committed
47

48 49 50 51 52 53
    public function getItemsIndexed()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItems')->indexBy('id');
    }

54 55 56 57 58 59
    public function getItemsWithNullFK()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->viaTable('order_item_with_null_fk', ['order_id' => 'id']);
    }

60 61 62 63 64 65 66
    public function getItemsInOrder1()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItems', function ($q) {
                $q->orderBy(['subtotal' => SORT_ASC]);
            })->orderBy('name');
    }
67

68 69 70 71 72 73 74
    public function getItemsInOrder2()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItems', function ($q) {
                $q->orderBy(['subtotal' => SORT_DESC]);
            })->orderBy('name');
    }
75

76 77 78
    public function getBooks()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
79
            ->via('orderItems')
80 81
            ->where(['category_id' => 1]);
    }
Qiang Xue committed
82

83
    public function getBooksWithNullFK()
84 85
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
Carsten Brandt committed
86
            ->via('orderItemsWithNullFK')
87 88 89 90 91 92 93 94 95 96 97
            ->where(['category_id' => 1]);
    }

    public function getBooksViaTable()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->viaTable('order_item', ['order_id' => 'id'])
            ->where(['category_id' => 1]);
    }

    public function getBooksWithNullFKViaTable()
98 99 100 101 102 103
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->viaTable('order_item_with_null_fk', ['order_id' => 'id'])
            ->where(['category_id' => 1]);
    }

104 105 106 107
    public function getBooks2()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->onCondition(['category_id' => 1])
108
            ->viaTable('order_item', ['order_id' => 'id']);
109
    }
110

111 112 113 114 115 116 117 118 119 120
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $this->created_at = time();

            return true;
        } else {
            return false;
        }
    }
Zander Baldwin committed
121
}