Order.php 1.31 KB
Newer Older
1 2 3 4 5 6
<?php

namespace yiiunit\data\ar\redis;

class Order extends ActiveRecord
{
7
	public function attributes()
8 9 10 11
	{
		return ['id', 'customer_id', 'create_time', 'total'];
	}

12 13
	public function getCustomer()
	{
14
		return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
15 16 17 18
	}

	public function getOrderItems()
	{
19
		return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
20 21 22 23
	}

	public function getItems()
	{
24
		return $this->hasMany(Item::className(), ['id' => 'item_id'])
25 26 27 28 29
			->via('orderItems', function($q) {
				// additional query configuration
			});
	}

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	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');
	}

46 47
	public function getBooks()
	{
48 49 50
		return $this->hasMany(Item::className(), ['id' => 'item_id'])
			->via('orderItems', ['order_id' => 'id']);
			//->where(['category_id' => 1]);
51 52 53 54 55 56 57 58 59 60 61 62
	}

	public function beforeSave($insert)
	{
		if (parent::beforeSave($insert)) {
			$this->create_time = time();
			return true;
		} else {
			return false;
		}
	}
}