Customer.php 2.23 KB
Newer Older
Qiang Xue committed
1 2
<?php
namespace yiiunit\data\ar;
Alexander Makarov committed
3

4
use yii\db\ActiveQuery;
5 6
use yiiunit\framework\db\ActiveRecordTest;

7 8 9 10 11 12 13 14
/**
 * Class Customer
 *
 * @property integer $id
 * @property string $name
 * @property string $email
 * @property string $address
 * @property integer $status
15 16
 *
 * @method CustomerQuery findBySql($sql, $params = []) static
17
 */
Qiang Xue committed
18 19
class Customer extends ActiveRecord
{
20 21
    const STATUS_ACTIVE = 1;
    const STATUS_INACTIVE = 2;
Qiang Xue committed
22

23
    public $status2;
Qiang Xue committed
24

25 26
    public static function tableName()
    {
27
        return 'customer';
28
    }
Qiang Xue committed
29

30 31 32 33
    public function getProfile()
    {
        return $this->hasOne(Profile::className(), ['id' => 'profile_id']);
    }
34

35 36 37 38
    public function getOrders()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id');
    }
39 40 41 42 43 44 45 46 47 48 49

    public function getExpensiveOrders()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])->andWhere('total > 50')->orderBy('id');
    }

    public function getExpensiveOrdersWithNullFK()
    {
        return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->andWhere('total > 50')->orderBy('id');
    }

50 51 52 53
    public function getOrdersWithNullFK()
    {
        return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->orderBy('id');
    }
54

55 56 57 58
    public function getOrders2()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id');
    }
59

60 61 62
    // deeply nested table relation
    public function getOrderItems()
    {
63
        /* @var $rel ActiveQuery */
64
        $rel = $this->hasMany(Item::className(), ['id' => 'item_id']);
65

66
        return $rel->viaTable('order_item', ['order_id' => 'id'], function ($q) {
67
            /* @var $q ActiveQuery */
68
            $q->viaTable('order', ['customer_id' => 'id']);
69 70
        })->orderBy('id');
    }
Alexander Makarov committed
71

72
    public function afterSave($insert, $changedAttributes)
73 74 75
    {
        ActiveRecordTest::$afterSaveInsert = $insert;
        ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
76
        parent::afterSave($insert, $changedAttributes);
77 78
    }

Alexander Makarov committed
79 80 81 82 83
    /**
     * @inheritdoc
     * @return CustomerQuery
     */
    public static function find()
84
    {
85
        return new CustomerQuery(get_called_class());
86
    }
Zander Baldwin committed
87
}