124 lines
2.8 KiB
PHP
124 lines
2.8 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace common\models;
|
||
|
|
||
|
use Yii;
|
||
|
|
||
|
/**
|
||
|
* This is the model class for table "product".
|
||
|
*
|
||
|
* @property int $id
|
||
|
* @property int|null $type
|
||
|
* @property string|null $name
|
||
|
* @property string|null $description
|
||
|
* @property string|null $photo
|
||
|
* @property float|null $price
|
||
|
* @property int|null $status
|
||
|
* @property int|null $quantity
|
||
|
* @property string|null $taste
|
||
|
* @property int|null $strength
|
||
|
* @property int|null $category_id
|
||
|
*
|
||
|
* @property Category $category
|
||
|
* @property OrderProduct[] $orderProducts
|
||
|
* @property Order[] $orders
|
||
|
*/
|
||
|
class Product extends \yii\db\ActiveRecord
|
||
|
{
|
||
|
|
||
|
const STATUS_ACTIVE = 1;
|
||
|
const STATUS_INACTIVE = 0;
|
||
|
const TYPE_TOBACCO = 1;
|
||
|
const TYPE_ELSE = 0;
|
||
|
const TOBACCO_STRENGTH_LITE = 0;
|
||
|
const TOBACCO_STRENGTH_MIDDLE = 1;
|
||
|
const TOBACCO_STRENGTH_HARD = 2;
|
||
|
/**
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public static function tableName()
|
||
|
{
|
||
|
return 'product';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public function rules()
|
||
|
{
|
||
|
return [
|
||
|
[['type', 'status', 'quantity', 'strength', 'category_id'], 'integer'],
|
||
|
[['price'], 'number'],
|
||
|
[['name', 'description', 'taste', 'photo'], 'string', 'max' => 255],
|
||
|
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::class, 'targetAttribute' => ['category_id' => 'id']],
|
||
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
public function attributeLabels()
|
||
|
{
|
||
|
return [
|
||
|
'id' => 'ID',
|
||
|
'type' => 'Type',
|
||
|
'name' => 'Name',
|
||
|
'description' => 'Description',
|
||
|
'price' => 'Price',
|
||
|
'status' => 'Status',
|
||
|
'quantity' => 'Quantity',
|
||
|
'taste' => 'Taste',
|
||
|
'strength' => 'Strength',
|
||
|
'category_id' => 'Category ID',
|
||
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets query for [[Category]].
|
||
|
*
|
||
|
* @return \yii\db\ActiveQuery
|
||
|
*/
|
||
|
public function getCategory()
|
||
|
{
|
||
|
return $this->hasOne(Category::class, ['id' => 'category_id']);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets query for [[OrderProducts]].
|
||
|
*
|
||
|
* @return \yii\db\ActiveQuery
|
||
|
*/
|
||
|
public function getOrderProducts()
|
||
|
{
|
||
|
return $this->hasMany(OrderProduct::class, ['product_id' => 'id']);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets query for [[Orders]].
|
||
|
*
|
||
|
* @return \yii\db\ActiveQuery
|
||
|
*/
|
||
|
public function getOrders()
|
||
|
{
|
||
|
return $this->hasMany(Order::class, ['id' => 'product_id'])
|
||
|
->viaTable('order_product', ['order_id' => 'id']);
|
||
|
}
|
||
|
|
||
|
public static function TypesWIthLabels()
|
||
|
{
|
||
|
return [
|
||
|
0 => 'другое',
|
||
|
1 => 'табак'
|
||
|
];
|
||
|
}
|
||
|
|
||
|
public static function TabacooStrengthWithLabel()
|
||
|
{
|
||
|
return [
|
||
|
0 => 'Легкий',
|
||
|
1 => 'Средний',
|
||
|
2 => 'Тяжелый'
|
||
|
];
|
||
|
}
|
||
|
}
|