<?php

namespace common\models;

use common\behaviors\SaveImgBehavior;
use common\modules\product\models\Category;
use common\modules\rubrication\models\TaxOption;
use Yii;

/**
 * This is the model class for table "category_to_purpose".
 *
 * @property integer $category_id
 * @property integer $purpose_id
 * @property string $image
 *
 * @property Category $category
 * @property TaxOption $purpose
 */
class CategoryToPurpose extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'category_to_purpose';
    }
    
    public static function primaryKey()
    {
        return [
            'category_id',
            'purpose_id'
        ];
    }
    
    public function behaviors()
    {
        return [
            [
                'class'  => SaveImgBehavior::className(),
                'fields' => [
                    [
                        'name'      => 'image',
                        'directory' => 'category_purpose',
                    ],
                ],
            ],
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['category_id', 'purpose_id'], 'required'],
            [['category_id', 'purpose_id'], 'integer'],
            [['image'], 'string', 'max' => 255],
            [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'category_id']],
            [['purpose_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxOption::className(), 'targetAttribute' => ['purpose_id' => 'tax_option_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'category_id' => Yii::t('app', 'Category ID'),
            'purpose_id' => Yii::t('app', 'Purpose ID'),
            'image' => Yii::t('app', 'Image'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Category::className(), ['category_id' => 'category_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPurpose()
    {
        return $this->hasOne(TaxOption::className(), ['tax_option_id' => 'purpose_id']);
    }
}
