Page.php 3.56 KB
<?php
    
    namespace artbox\core\models;
    
    use artbox\core\behaviors\GalleryBehavior;
    use artbox\core\behaviors\LanguageBehavior;
    use artbox\core\behaviors\ManyToManyBehavior;
    use yii\db\ActiveQuery;
    use yii\db\ActiveRecord;
    use yii\web\Request;
    use Yii;
    
    /**
     * This is the model class for table "page".
     *
     * @property integer        $id
     * @property bool           $in_menu
     * @property integer        $sort
     * @property PageCategory[] $categories
     *      * From language behavior *
     * @property PageLang       $lang
     * @property PageLang[]     $langs
     * @property PageLang       $objectLang
     * @property string         $ownerKey
     * @property string         $langKey
     * @property PageLang[]     $modelLangs
     * @property bool           $transactionStatus
     * @method string       getOwnerKey()
     * @method void         setOwnerKey( string $value )
     * @method string       getLangKey()
     * @method void         setLangKey( string $value )
     * @method ActiveQuery  getLangs()
     * @method ActiveQuery  getLang( integer $language_id )
     * @method PageLang[]   generateLangs()
     * @method void         loadLangs( Request $request )
     * @method bool         linkLangs()
     * @method bool         saveLangs()
     * @method bool         getTransactionStatus()
     * @method bool         loadWithLangs( Request $request )
     * @method bool         saveWithLangs()
     *      * End language behavior *
     * @see LanguageBehavior
     * @method bool saveImages( array $post )
     * @method Image[] getImages()
     *      * Many to many *
     * @method void linkMany( string $link, array $models )
     */
    class Page extends ActiveRecord
    {
        /**
         * @var array
         */
        public $categoryIds;
        
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'page';
        }
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'language'   => [
                    'class' => LanguageBehavior::className(),
                ],
                'gallery'    => [
                    'class' => GalleryBehavior::className(),
                ],
                'manyToMany' => [
                    'class' => ManyToManyBehavior::className(),
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'in_menu',
                    ],
                    'boolean',
                ],
                [
                    [
                        'sort',
                    ],
                    'integer',
                    'min' => 0,
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id'      => Yii::t('core', 'page_id'),
                'in_menu' => Yii::t('core', 'page_in_menu'),
                'sort'    => Yii::t('core', 'page_sort'),
                'title'   => Yii::t('core', 'page_title'),
            ];
        }
        
        /**
         * @return ActiveQuery
         */
        public function getCategories()
        {
            return $this->hasMany(PageCategory::className(), [ 'id' => 'category_id' ])
                        ->viaTable('page_to_category', [ 'page_id' => 'id' ]);
        }
    }