TimestampBehavior::className(), 'updatedAtAttribute' => false, ], [ 'class' => SlugBehavior::className(), 'action' => 'book/view', 'params' => [ 'id' => 'id', ], ] ]; } /** * {@inheritdoc} */ public function rules() { return [ [ [ 'author_id', 'created_at', 'status', ], 'default', 'value' => null, ], [ [ 'author_id', 'created_at', 'status', 'alias_id' ], 'integer', ], [ [ 'preview', 'description', ], 'string', ], [ [ 'title', 'image', ], 'string', 'max' => 255, ], [ [ 'author_id' ], 'exist', 'skipOnError' => true, 'targetClass' => Author::className(), 'targetAttribute' => [ 'author_id' => 'id' ], ], [ ['title'], 'required' ], [ ['on_main'], 'boolean' ] ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => Yii::t('app', 'ID'), 'author_id' => Yii::t('app', 'Author ID'), 'title' => Yii::t('app', 'Title'), 'image' => Yii::t('app', 'Image'), 'preview' => Yii::t('app', 'Preview'), 'description' => Yii::t('app', 'Description'), 'created_at' => Yii::t('app', 'Created At'), 'status' => Yii::t('app', 'Status'), ]; } /** * @return \yii\db\ActiveQuery */ public function getAuthor() { return $this->hasOne(Author::className(), [ 'id' => 'author_id' ]); } public function getStatuses(): array{ return [ self::STATUS_ACTIVE => 'Активный', self::STATUS_MODERATION => 'На модерации', self::STATUS_DELETED => 'На удалении', ]; } public function saveImage($file){ /** * @var UploadedFile $file; */ if (!empty($file)){ if (!file_exists(\Yii::getAlias('@storage/books/') . $this->id)) { FileHelper::createDirectory(\Yii::getAlias('@storage/books/') . $this->id); } if ($file->saveAs(\Yii::getAlias('@storage/books/').$this->id.'/'.$this->id.'.'.$file->extension)){ $this->image = $this->id.'.'.$file->extension; return $this->save(); } return false; } return true; } public function getAlias(){ return $this->hasOne(Alias::className(), ['id' => 'alias_id']); } public function getActiveComments(){ return $this->hasMany(Comment::className(), ['entity_id' => 'id'])->andWhere(['entity' => Book::className()])->with('activeComments')->where(['status' => true, 'parent_id' => null])->orderBy('created_at'); } }