<?php

namespace common\modules\product\models;

use Yii;

/**
 * This is the model class for table "remote_categories".
 *
 * @property string $Name
 * @property string $ID_chief
 * @property string $ID
 */
class RemoteCategories extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'remote_categories';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Name'], 'string', 'max' => 100],
            [['ID_chief', 'ID'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'Name' => Yii::t('product', 'Name'),
            'ID_chief' => Yii::t('product', 'Id Chief'),
            'ID' => Yii::t('product', 'ID'),
        ];
    }

    public function getCategory() {
        if (empty($this->ID)) {
            return null;
        }
        return CategorySearch::findByRemoteID($this->ID);
    }

    public static function findByID($id) {
        /** @var CategoryQuery $query */
        $query = RemoteCategories::find()
            ->andFilterWhere(['ID' => $id]);
        if (($model = $query->one()) !== null) {
            return $model;
        }
        return null;
    }
}
