Order.php 1.76 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
{
Qiang Xue committed
15
	public static function tableName()
Qiang Xue committed
16 17 18
	{
		return 'tbl_order';
	}
Qiang Xue committed
19

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

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

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

Qiang Xue committed
35
	public function getItems()
Qiang Xue committed
36
	{
37
		return $this->hasMany(Item::className(), ['id' => 'item_id'])
Alexander Makarov committed
38
			->via('orderItems', function ($q) {
39 40
				// additional query configuration
			})->orderBy('id');
Qiang Xue committed
41 42
	}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	public function getItemsInOrder1()
	{
		return $this->hasMany(Item::className(), ['id' => 'item_id'])
			->via('orderItems', function ($q) {
				$q->orderBy(['subtotal' => SORT_ASC]);
			})->orderBy('name');
	}

	public function getItemsInOrder2()
	{
		return $this->hasMany(Item::className(), ['id' => 'item_id'])
			->via('orderItems', function ($q) {
				$q->orderBy(['subtotal' => SORT_DESC]);
			})->orderBy('name');
	}

Qiang Xue committed
59
	public function getBooks()
Qiang Xue committed
60
	{
61
		return $this->hasMany(Item::className(), ['id' => 'item_id'])
Alexander Makarov committed
62 63
			->viaTable('tbl_order_item', ['order_id' => 'id'])
			->where(['category_id' => 1]);
Qiang Xue committed
64
	}
Qiang Xue committed
65

66 67 68 69 70 71 72
	public function getBooks2()
	{
		return $this->hasMany(Item::className(), ['id' => 'item_id'])
			->onCondition(['category_id' => 1])
			->viaTable('tbl_order_item', ['order_id' => 'id']);
	}

Qiang Xue committed
73 74 75
	public function beforeSave($insert)
	{
		if (parent::beforeSave($insert)) {
Alexander Kochetov committed
76
			$this->created_at = time();
Qiang Xue committed
77 78 79 80 81
			return true;
		} else {
			return false;
		}
	}
Zander Baldwin committed
82
}