Customer.php 1.69 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
    public function getOrders2()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id');
    }
44

45 46 47 48 49
    // deeply nested table relation
    public function getOrderItems()
    {
        /** @var ActiveQuery $rel */
        $rel = $this->hasMany(Item::className(), ['id' => 'item_id']);
50

51
        return $rel->viaTable('order_item', ['order_id' => 'id'], function ($q) {
52
            /** @var ActiveQuery $q */
53
            $q->viaTable('order', ['customer_id' => 'id']);
54 55
        })->orderBy('id');
    }
Alexander Makarov committed
56

57 58 59 60 61 62 63
    public function afterSave($insert)
    {
        ActiveRecordTest::$afterSaveInsert = $insert;
        ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
        parent::afterSave($insert);
    }

Alexander Makarov committed
64 65 66 67 68
    /**
     * @inheritdoc
     * @return CustomerQuery
     */
    public static function find()
69
    {
70
        return new CustomerQuery(get_called_class());
71
    }
Zander Baldwin committed
72
}