Comment.php 3.63 KB
<?php
    
    namespace common\models;
    
    use Yii;
    use yii\behaviors\TimestampBehavior;
    
    /**
     * This is the model class for table "comment".
     *
     * @property int       $id
     * @property int       $book_id
     * @property string    $name
     * @property string    $email
     * @property string    $comment
     * @property int       $parent_id
     * @property Book      $book
     * @property Comment   $parent
     * @property Comment[] $comments
     */
    class Comment extends \yii\db\ActiveRecord
    {
        /**
         * {@inheritdoc}
         */
        public static function tableName()
        {
            return 'comment';
        }
        public function behaviors()
        {
            return [
                [
                    'class'              => TimestampBehavior::className(),
                    'updatedAtAttribute' => false,
                ],
            ];
            
        }
        
        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                [
                    [
                        'book_id',
                        'parent_id',
                    ],
                    'default',
                    'value' => null,
                ],
                [
                    [
                        'book_id',
                        'parent_id',
                    ],
                    'integer',
                ],
                [
                    [ 'comment' ],
                    'string',
                ],
                [
                    'email',
                    'email',
                ],
                [
                    [
                        'name',
                        'email',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'book_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Book::className(),
                    'targetAttribute' => [ 'book_id' => 'id' ],
                ],
                [
                    [ 'parent_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Comment::className(),
                    'targetAttribute' => [ 'parent_id' => 'id' ],
                ],
            ];
        }
        
        /**
         * {@inheritdoc}
         */
        public function attributeLabels()
        {
            return [
                'id'        => Yii::t('app', 'ID'),
                'book_id'   => Yii::t('app', 'Book ID'),
                'name'      => Yii::t('app', 'Name'),
                'email'     => Yii::t('app', 'Email'),
                'comment'   => Yii::t('app', 'Comment'),
                'parent_id' => Yii::t('app', 'Parent ID'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getBook()
        {
            return $this->hasOne(Book::className(), [ 'id' => 'book_id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getParent()
        {
            return $this->hasOne(Comment::className(), [ 'id' => 'parent_id' ]);
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getComments()
        {
            return $this->hasMany(Comment::className(), [ 'parent_id' => 'id' ]);
        }
        
        public function getActiveComments(){
            return $this->getComments()->where(['status' => true]);
        }
    }