Order.php 2.04 KB
Newer Older
1 2 3
<?php

namespace yiiunit\data\ar\elasticsearch;
4
use yii\elasticsearch\Command;
5 6 7 8 9 10

/**
 * Class Order
 *
 * @property integer $id
 * @property integer $customer_id
Alexander Kochetov committed
11
 * @property integer $created_at
12 13 14 15
 * @property string $total
 */
class Order extends ActiveRecord
{
16 17
	public static function primaryKey()
	{
18
		return ['id'];
19 20
	}

21
	public function attributes()
22
	{
Alexander Kochetov committed
23
		return ['id', 'customer_id', 'created_at', 'total'];
24 25 26 27
	}

	public function getCustomer()
	{
28
		return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
29 30 31 32
	}

	public function getOrderItems()
	{
33
		return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
34 35 36 37
	}

	public function getItems()
	{
38
		return $this->hasMany(Item::className(), ['id' => 'item_id'])
39
			->via('orderItems')->orderBy('id');
40 41
	}

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	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');
	}

58 59
//	public function getBooks()
//	{
60 61
//		return $this->hasMany('Item', ['id' => 'item_id'])
//			->viaTable('tbl_order_item', ['order_id' => 'id'])
62 63
//			->where(['category_id' => 1]);
//	}
64 65 66 67

	public function beforeSave($insert)
	{
		if (parent::beforeSave($insert)) {
Alexander Kochetov committed
68
//			$this->created_at = time();
69 70 71 72 73
			return true;
		} else {
			return false;
		}
	}
74 75 76 77 78 79 80 81 82 83 84 85 86

	/**
	 * sets up the index for this record
	 * @param Command $command
	 */
	public static function setUpMapping($command)
	{
		$command->deleteMapping(static::index(), static::type());
		$command->setMapping(static::index(), static::type(), [
			static::type() => [
				"_id" => ["path" => "id", "index" => "not_analyzed", "store" => "yes"],
				"properties" => [
					"customer_id" => ["type" => "integer"],
Alexander Kochetov committed
87
//					"created_at" => ["type" => "string", "index" => "not_analyzed"],
88 89 90 91 92 93
					"total" => ["type" => "integer"],
				]
			]
		]);

	}
94
}