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

namespace yiiunit\data\ar\elasticsearch;
AlexGx committed
4

5
use yii\elasticsearch\Command;
6 7 8 9 10 11

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

22 23
    public function attributes()
    {
24
        return ['id', 'customer_id', 'created_at', 'total', 'itemsArray'];
25
    }
26

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

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

37 38 39 40 41 42 43 44
    /**
     * A relation to Item defined via array valued attribute
     */
    public function getItemsByArrayValue()
    {
        return $this->hasMany(Item::className(), ['id' => 'itemsArray'])->indexBy('id');
    }

45 46 47 48 49
    public function getItems()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItems')->orderBy('id');
    }
50

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

57 58 59 60 61 62 63 64 65 66 67
    public function getItemsWithNullFK()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItemsWithNullFK');
    }

    public function getOrderItemsWithNullFK()
    {
        return $this->hasMany(OrderItemWithNullFK::className(), ['order_id' => 'id']);
    }

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

76 77 78 79 80 81 82
    public function getItemsInOrder2()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItems', function ($q) {
                $q->orderBy(['subtotal' => SORT_DESC]);
            })->orderBy('name');
    }
83

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

    public function getBooksWithNullFK()
    {
        return $this->hasMany(Item::className(), ['id' => 'item_id'])
            ->via('orderItemsWithNullFK')
            ->where(['category_id' => 1]);
    }
97

98 99 100
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
Alexander Kochetov committed
101
//			$this->created_at = time();
102 103 104 105 106
            return true;
        } else {
            return false;
        }
    }
107

108 109 110 111 112 113 114 115 116 117 118 119
    /**
     * 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
120
//					"created_at" => ["type" => "string", "index" => "not_analyzed"],
121 122 123 124 125
                    "total" => ["type" => "integer"],
                ]
            ]
        ]);
    }
126
}