TimestampBehavior::class, 'createdAtAttribute' => 'created_at', 'updatedAtAttribute' => 'updated_at', 'value' => new Expression('NOW()'), ], ]; } /** * {@inheritdoc} */ public function rules() { return [ [['category_id', 'status', 'title'], 'required'], [['category_id', 'status'], 'integer'], [['created_at', 'updated_at', 'time_limit'], 'safe'], ['title', 'unique'], [['title', 'description'], 'string', 'max' => 255], ['status', 'default', 'value' => true], [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => QuestionnaireCategory::className(), 'targetAttribute' => ['category_id' => 'id']], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'category_id' => 'Категория', 'title' => 'Название анкеты', 'description' => 'Описание', 'status' => 'Статус', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'time_limit' => 'Время на выполнение (HH:mm)', ]; } public function beforeSave($insert): bool { if (parent::beforeSave($insert)) { if (strtotime($this->time_limit, '0') === 0) { $this->time_limit = null; } return true; } else { return false; } } /** * @return ActiveQuery */ public function getQuestions() { return $this->hasMany(Question::className(), ['questionnaire_id' => 'id']); } /** * @return ActiveQuery */ public function getAnswers() { return $this->hasMany(Answer::className(), ['question_id' => 'id']) ->viaTable('question', ['questionnaire_id' => 'id']); } /** * @return ActiveQuery */ public function getCategory(): ActiveQuery { return $this->hasOne(QuestionnaireCategory::className(), ['id' => 'category_id']); } /** * @return ActiveQuery */ public function getUserQuestionnaires() { return $this->hasMany(UserQuestionnaire::className(), ['questionnaire_id' => 'id']); } public static function questionnairesByCategoryArr($category_id): array { $categories = self::find()->where(['category_id' => $category_id, 'status' => '1'])->all(); return ArrayHelper::map($categories, 'id', 'title'); } }