Buy.php 2.67 KB
<?php
    
    namespace common\models;
    
    use Yii;
    use yii\behaviors\TimestampBehavior;
    
    /**
     * This is the model class for table "buy".
     *
     * @property int    $id
     * @property int    $book_id
     * @property string $name
     * @property string $email
     * @property int    $created_at
     * @property int    $status
     * @property Book   $book
     */
    class Buy extends \yii\db\ActiveRecord
    {
        /**
         * {@inheritdoc}
         */
        public static function tableName()
        {
            return 'buy';
        }
        public function behaviors()
        {
            return [
                [
                    'class'              => TimestampBehavior::className(),
                    'updatedAtAttribute' => false,
                ],
            ];
            
        }
        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                [
                    [
                        'book_id',
                        'created_at',
                        'status',
                    ],
                    'default',
                    'value' => null,
                ],
                [
                    [
                        'book_id',
                        'created_at',
                        'status',
                    ],
                    'integer',
                ],
                [
                    [
                        'name',
                        'email',
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                    [ 'book_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Book::className(),
                    'targetAttribute' => [ 'book_id' => 'id' ],
                ],
                [
                    ['email'], 'required'
                ]
            ];
        }
        
        /**
         * {@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'),
                'created_at' => Yii::t('app', 'Created At'),
                'status'     => Yii::t('app', 'Status'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getBook()
        {
            return $this->hasOne(Book::className(), [ 'id' => 'book_id' ]);
        }
    }