Order.php 921 Bytes
Newer Older
1 2 3 4 5 6
<?php

namespace yiiunit\data\ar\redis;

class Order extends ActiveRecord
{
7 8 9 10 11
	public static function attributes()
	{
		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 30 31
			->via('orderItems', function($q) {
				// additional query configuration
			});
	}

	public function getBooks()
	{
32 33 34
		return $this->hasMany(Item::className(), ['id' => 'item_id'])
			->via('orderItems', ['order_id' => 'id']);
			//->where(['category_id' => 1]);
35 36 37 38 39 40 41 42 43 44 45 46
	}

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