You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.2 KiB
88 lines
2.2 KiB
13 years ago
|
<?php
|
||
|
namespace yiiunit\data\ar;
|
||
11 years ago
|
|
||
11 years ago
|
use yii\db\ActiveQuery;
|
||
11 years ago
|
use yiiunit\framework\db\ActiveRecordTest;
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Class Customer
|
||
|
*
|
||
|
* @property integer $id
|
||
|
* @property string $name
|
||
|
* @property string $email
|
||
|
* @property string $address
|
||
|
* @property integer $status
|
||
11 years ago
|
*
|
||
|
* @method CustomerQuery findBySql($sql, $params = []) static
|
||
11 years ago
|
*/
|
||
13 years ago
|
class Customer extends ActiveRecord
|
||
|
{
|
||
11 years ago
|
const STATUS_ACTIVE = 1;
|
||
|
const STATUS_INACTIVE = 2;
|
||
13 years ago
|
|
||
11 years ago
|
public $status2;
|
||
12 years ago
|
|
||
11 years ago
|
public static function tableName()
|
||
|
{
|
||
11 years ago
|
return 'customer';
|
||
11 years ago
|
}
|
||
13 years ago
|
|
||
11 years ago
|
public function getProfile()
|
||
|
{
|
||
|
return $this->hasOne(Profile::className(), ['id' => 'profile_id']);
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
public function getOrders()
|
||
|
{
|
||
|
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->orderBy('id');
|
||
|
}
|
||
10 years ago
|
|
||
|
public function getExpensiveOrders()
|
||
|
{
|
||
10 years ago
|
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id');
|
||
10 years ago
|
}
|
||
|
|
||
|
public function getExpensiveOrdersWithNullFK()
|
||
|
{
|
||
10 years ago
|
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->andWhere('[[total]] > 50')->orderBy('id');
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
public function getOrdersWithNullFK()
|
||
|
{
|
||
|
return $this->hasMany(OrderWithNullFK::className(), ['customer_id' => 'id'])->orderBy('id');
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
public function getOrders2()
|
||
|
{
|
||
|
return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer2')->orderBy('id');
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// deeply nested table relation
|
||
|
public function getOrderItems()
|
||
|
{
|
||
10 years ago
|
/* @var $rel ActiveQuery */
|
||
11 years ago
|
$rel = $this->hasMany(Item::className(), ['id' => 'item_id']);
|
||
11 years ago
|
|
||
11 years ago
|
return $rel->viaTable('order_item', ['order_id' => 'id'], function ($q) {
|
||
10 years ago
|
/* @var $q ActiveQuery */
|
||
11 years ago
|
$q->viaTable('order', ['customer_id' => 'id']);
|
||
11 years ago
|
})->orderBy('id');
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
public function afterSave($insert, $changedAttributes)
|
||
11 years ago
|
{
|
||
|
ActiveRecordTest::$afterSaveInsert = $insert;
|
||
|
ActiveRecordTest::$afterSaveNewRecord = $this->isNewRecord;
|
||
10 years ago
|
parent::afterSave($insert, $changedAttributes);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* @inheritdoc
|
||
|
* @return CustomerQuery
|
||
|
*/
|
||
|
public static function find()
|
||
11 years ago
|
{
|
||
11 years ago
|
return new CustomerQuery(get_called_class());
|
||
11 years ago
|
}
|
||
12 years ago
|
}
|