itstructure /
yii2-template-simple
| 1 | <?php |
||
| 2 | |||
| 3 | namespace app\models; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * This is the model class for table "home". |
||
| 9 | * |
||
| 10 | * @property int $id |
||
| 11 | * @property int $default |
||
| 12 | * @property string $title |
||
| 13 | * @property string $description |
||
| 14 | * @property string $content |
||
| 15 | * @property string $metaKeys |
||
| 16 | * @property string $metaDescription |
||
| 17 | * @property string $created_at |
||
| 18 | * @property string $updated_at |
||
| 19 | * |
||
| 20 | * @package app\models |
||
| 21 | */ |
||
| 22 | class Home extends ActiveRecord |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * {@inheritdoc} |
||
| 26 | */ |
||
| 27 | public static function tableName() |
||
| 28 | { |
||
| 29 | return 'home'; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | public function rules() |
||
| 36 | { |
||
| 37 | return [ |
||
| 38 | [ |
||
| 39 | [ |
||
| 40 | 'title', |
||
| 41 | ], |
||
| 42 | 'required' |
||
| 43 | ], |
||
| 44 | [ |
||
| 45 | [ |
||
| 46 | 'description', |
||
| 47 | 'content' |
||
| 48 | ], |
||
| 49 | 'string' |
||
| 50 | ], |
||
| 51 | [ |
||
| 52 | [ |
||
| 53 | 'title', |
||
| 54 | 'metaKeys', |
||
| 55 | ], |
||
| 56 | 'string', |
||
| 57 | 'max' => 128 |
||
| 58 | ], |
||
| 59 | [ |
||
| 60 | [ |
||
| 61 | 'metaDescription' |
||
| 62 | ], |
||
| 63 | 'string', |
||
| 64 | 'max' => 255 |
||
| 65 | ], |
||
| 66 | [ |
||
| 67 | [ |
||
| 68 | 'default' |
||
| 69 | ], |
||
| 70 | 'integer' |
||
| 71 | ], |
||
| 72 | [ |
||
| 73 | 'title', |
||
| 74 | 'unique', |
||
| 75 | 'skipOnError' => true, |
||
| 76 | 'targetClass' => static::class, |
||
| 77 | 'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : '' |
||
| 78 | ], |
||
| 79 | [ |
||
| 80 | [ |
||
| 81 | 'created_at', |
||
| 82 | 'updated_at' |
||
| 83 | ], |
||
| 84 | 'safe' |
||
| 85 | ], |
||
| 86 | ]; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function attributeLabels() |
||
| 93 | { |
||
| 94 | return [ |
||
| 95 | 'id' => 'ID', |
||
| 96 | 'default' => Yii::t('app', 'Default'), |
||
| 97 | 'title' => Yii::t('app', 'Title'), |
||
| 98 | 'description' => Yii::t('app', 'Description'), |
||
| 99 | 'content' => Yii::t('app', 'Content'), |
||
| 100 | 'metaKeys' => Yii::t('app', 'Meta keys'), |
||
| 101 | 'metaDescription' => Yii::t('app', 'Meta description'), |
||
| 102 | 'created_at' => Yii::t('app', 'Created date'), |
||
| 103 | 'updated_at' => Yii::t('app', 'Updated date'), |
||
| 104 | ]; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Reset the default home record. |
||
| 109 | * |
||
| 110 | * @param boolean $insert |
||
| 111 | * |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | public function beforeSave($insert) |
||
| 115 | { |
||
| 116 | if ($this->default == 1) { |
||
| 117 | |||
| 118 | $default = static::findOne([ |
||
| 119 | 'default' => 1, |
||
| 120 | ]); |
||
| 121 | |||
| 122 | if (null !== $default) { |
||
| 123 | $default->default = 0; |
||
| 124 | $default->save(); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | return parent::beforeSave($insert); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the default home record. |
||
| 133 | * |
||
| 134 | * @return null|\yii\db\ActiveRecord |
||
| 135 | */ |
||
| 136 | public static function getDefaultHome() |
||
| 137 | { |
||
| 138 | return static::find() |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 139 | ->where([ |
||
| 140 | 'default' => 1 |
||
| 141 | ]) |
||
| 142 | ->one(); |
||
| 143 | } |
||
| 144 | } |
||
| 145 |