Customer.php 1.59 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 17
 *
 * @method CustomerQuery|Customer|null find($q = null) static
 * @method CustomerQuery findBySql($sql, $params = []) static
18
 */
Qiang Xue committed
19 20
class Customer extends ActiveRecord
{
Qiang Xue committed
21 22 23
	const STATUS_ACTIVE = 1;
	const STATUS_INACTIVE = 2;

Qiang Xue committed
24 25
	public $status2;

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

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

Qiang Xue committed
36
	public function getOrders()
Qiang Xue committed
37
	{
38
		return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id');
Qiang Xue committed
39
	}
40

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

46 47 48 49 50 51 52 53 54 55 56
	// deeply nested table relation
	public function getOrderItems()
	{
		/** @var ActiveQuery $rel */
		$rel = $this->hasMany(Item::className(), ['id' => 'item_id']);
		return $rel->viaTable('tbl_order_item', ['order_id' => 'id'], function($q) {
			/** @var ActiveQuery $q */
			$q->viaTable('tbl_order', ['customer_id' => 'id']);
		})->orderBy('id');
	}

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

64
	public static function createQuery($config = [])
Alexander Makarov committed
65
	{
66 67
		$config['modelClass'] = get_called_class();
		return new CustomerQuery($config);
Alexander Makarov committed
68
	}
Zander Baldwin committed
69
}