Customer.php 1.97 KB
Newer Older
1 2 3
<?php
namespace yiiunit\data\ar\elasticsearch;

4
use yii\elasticsearch\Command;
5
use yiiunit\extensions\elasticsearch\ActiveRecordTest;
6

7 8 9 10 11 12 13 14 15 16 17
/**
 * Class Customer
 *
 * @property integer $id
 * @property string $name
 * @property string $email
 * @property string $address
 * @property integer $status
 */
class Customer extends ActiveRecord
{
18 19
    const STATUS_ACTIVE = 1;
    const STATUS_INACTIVE = 2;
20

21
    public $status2;
22

23 24 25 26
    public static function primaryKey()
    {
        return ['id'];
    }
27

28 29 30 31
    public function attributes()
    {
        return ['id', 'name', 'email', 'address', 'status'];
    }
32

33 34 35 36
    public function getOrders()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('created_at');
    }
37

38 39 40 41 42 43
    public function afterSave($insert)
    {
        ActiveRecordTest::$afterSaveInsert = $insert;
        ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
        parent::afterSave($insert);
    }
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    /**
     * sets up the index for this record
     * @param Command $command
     * @param boolean $statusIsBoolean
     */
    public static function setUpMapping($command, $statusIsBoolean = false)
    {
        $command->deleteMapping(static::index(), static::type());
        $command->setMapping(static::index(), static::type(), [
            static::type() => [
                "_id" => ["path" => "id", "index" => "not_analyzed", "store" => "yes"],
                "properties" => [
                    "name" =>        ["type" => "string", "index" => "not_analyzed"],
                    "email" =>       ["type" => "string", "index" => "not_analyzed"],
                    "address" =>     ["type" => "string", "index" => "analyzed"],
                    "status" => $statusIsBoolean ? ["type" => "boolean"] : ["type" => "integer"],
                ]
            ]
        ]);
64

65
    }
66

Alexander Makarov committed
67 68 69 70 71
    /**
     * @inheritdoc
     * @return CustomerQuery
     */
    public static function find()
72
    {
73
        return new CustomerQuery(get_called_class());
74
    }
75
}