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.
130 lines
3.4 KiB
130 lines
3.4 KiB
13 years ago
|
<?php
|
||
|
|
||
|
namespace yiiunit\data\ar;
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Class Order
|
||
|
*
|
||
|
* @property integer $id
|
||
|
* @property integer $customer_id
|
||
11 years ago
|
* @property integer $created_at
|
||
11 years ago
|
* @property string $total
|
||
|
*/
|
||
13 years ago
|
class Order extends ActiveRecord
|
||
|
{
|
||
11 years ago
|
public static function tableName()
|
||
|
{
|
||
11 years ago
|
return 'order';
|
||
11 years ago
|
}
|
||
13 years ago
|
|
||
11 years ago
|
public function getCustomer()
|
||
|
{
|
||
|
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
public function getCustomer2()
|
||
|
{
|
||
|
return $this->hasOne(Customer::className(), ['id' => 'customer_id'])->inverseOf('orders2');
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
public function getOrderItems()
|
||
|
{
|
||
|
return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
public function getOrderItemsWithNullFK()
|
||
|
{
|
||
|
return $this->hasMany(OrderItemWithNullFK::className(), ['order_id' => 'id']);
|
||
|
}
|
||
|
|
||
11 years ago
|
public function getItems()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
|
->via('orderItems', function ($q) {
|
||
|
// additional query configuration
|
||
11 years ago
|
})->orderBy('item.id');
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
10 years ago
|
public function getItemsIndexed()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
|
->via('orderItems')->indexBy('id');
|
||
|
}
|
||
|
|
||
10 years ago
|
public function getItemsWithNullFK()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
|
->viaTable('order_item_with_null_fk', ['order_id' => 'id']);
|
||
|
}
|
||
|
|
||
11 years ago
|
public function getItemsInOrder1()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
|
->via('orderItems', function ($q) {
|
||
|
$q->orderBy(['subtotal' => SORT_ASC]);
|
||
|
})->orderBy('name');
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
public function getItemsInOrder2()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
|
->via('orderItems', function ($q) {
|
||
|
$q->orderBy(['subtotal' => SORT_DESC]);
|
||
|
})->orderBy('name');
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
public function getBooks()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
10 years ago
|
->via('orderItems')
|
||
11 years ago
|
->where(['category_id' => 1]);
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
public function getBooksWithNullFK()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
10 years ago
|
->via('orderItemsWithNullFK')
|
||
10 years ago
|
->where(['category_id' => 1]);
|
||
|
}
|
||
|
|
||
|
public function getBooksViaTable()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
|
->viaTable('order_item', ['order_id' => 'id'])
|
||
|
->where(['category_id' => 1]);
|
||
|
}
|
||
|
|
||
|
public function getBooksWithNullFKViaTable()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
10 years ago
|
->viaTable('order_item_with_null_fk', ['order_id' => 'id'])
|
||
|
->where(['category_id' => 1]);
|
||
|
}
|
||
|
|
||
11 years ago
|
public function getBooks2()
|
||
|
{
|
||
|
return $this->hasMany(Item::className(), ['id' => 'item_id'])
|
||
|
->onCondition(['category_id' => 1])
|
||
11 years ago
|
->viaTable('order_item', ['order_id' => 'id']);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
public function beforeSave($insert)
|
||
|
{
|
||
|
if (parent::beforeSave($insert)) {
|
||
|
$this->created_at = time();
|
||
|
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
public function attributeLabels()
|
||
|
{
|
||
|
return [
|
||
|
'customer_id' => 'Customer',
|
||
|
'total' => 'Invoice Total',
|
||
|
];
|
||
|
}
|
||
12 years ago
|
}
|