Customer.php 1.84 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
    public function getOrdersWithNullFK()
    {
        return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->orderBy('id');
    }
43

44 45 46 47
    public function getOrders2()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id');
    }
48

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

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

61 62 63 64 65 66 67
    public function afterSave($insert)
    {
        ActiveRecordTest::$afterSaveInsert = $insert;
        ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
        parent::afterSave($insert);
    }

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