itstructure /
yii2-template-multilanguage
| 1 | <?php |
||
| 2 | |||
| 3 | namespace app\models; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use Itstructure\AdminModule\models\{MultilanguageTrait, Language, ActiveRecord}; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * This is the model class for table "home". |
||
| 10 | * |
||
| 11 | * @property int $id |
||
| 12 | * @property int $default |
||
| 13 | * @property string $created_at |
||
| 14 | * @property string $updated_at |
||
| 15 | * |
||
| 16 | * @property HomeLanguage[] $homeLanguages |
||
| 17 | * @property Language[] $languages |
||
| 18 | * |
||
| 19 | * @package app\models |
||
| 20 | */ |
||
| 21 | class Home extends ActiveRecord |
||
| 22 | { |
||
| 23 | use MultilanguageTrait; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 24 | |||
| 25 | /** |
||
| 26 | * {@inheritdoc} |
||
| 27 | */ |
||
| 28 | public static function tableName() |
||
| 29 | { |
||
| 30 | return 'home'; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function rules() |
||
| 37 | { |
||
| 38 | return [ |
||
| 39 | [ |
||
| 40 | [ |
||
| 41 | 'default' |
||
| 42 | ], |
||
| 43 | 'integer' |
||
| 44 | ], |
||
| 45 | [ |
||
| 46 | [ |
||
| 47 | 'created_at', |
||
| 48 | 'updated_at' |
||
| 49 | ], |
||
| 50 | 'safe' |
||
| 51 | ], |
||
| 52 | ]; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | public function attributeLabels() |
||
| 59 | { |
||
| 60 | return [ |
||
| 61 | 'id' => 'ID', |
||
| 62 | 'default' => Yii::t('app', 'Default'), |
||
| 63 | 'created_at' => Yii::t('app', 'Created date'), |
||
| 64 | 'updated_at' => Yii::t('app', 'Updated date'), |
||
| 65 | ]; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Reset the default home record. |
||
| 70 | * |
||
| 71 | * @param boolean $insert |
||
| 72 | * |
||
| 73 | * @return mixed |
||
| 74 | */ |
||
| 75 | public function beforeSave($insert) |
||
| 76 | { |
||
| 77 | if ($this->default == 1) { |
||
| 78 | |||
| 79 | $default = static::findOne([ |
||
| 80 | 'default' => 1, |
||
| 81 | ]); |
||
| 82 | |||
| 83 | if (null !== $default) { |
||
| 84 | $default->default = 0; |
||
| 85 | $default->save(); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return parent::beforeSave($insert); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the default home record. |
||
| 94 | * |
||
| 95 | * @return array|null|\yii\db\ActiveRecord |
||
| 96 | */ |
||
| 97 | public static function getDefaultHome() |
||
| 98 | { |
||
| 99 | return static::find() |
||
| 100 | ->where([ |
||
| 101 | 'default' => 1 |
||
| 102 | ]) |
||
| 103 | ->one(); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return \yii\db\ActiveQuery |
||
| 108 | */ |
||
| 109 | public function getHomeLanguages() |
||
| 110 | { |
||
| 111 | return $this->hasMany(HomeLanguage::class, [ |
||
| 112 | 'home_id' => 'id' |
||
| 113 | ]); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return \yii\db\ActiveQuery |
||
| 118 | */ |
||
| 119 | public function getLanguages() |
||
| 120 | { |
||
| 121 | return $this->hasMany(Language::class, [ |
||
| 122 | 'id' => 'language_id' |
||
| 123 | ])->viaTable('home_language', [ |
||
| 124 | 'home_id' => 'id' |
||
| 125 | ]); |
||
| 126 | } |
||
| 127 | } |
||
| 128 |