Current Path : /home/vmanager/www/common/models/ |
Linux 9dbcd5f6333d 5.15.0-124-generic #134-Ubuntu SMP Fri Sep 27 20:20:17 UTC 2024 x86_64 |
Current File : /home/vmanager/www/common/models/WebsitesPagesHasWebsitesPhotos.php |
<?php namespace common\models; use Yii; use yii\db\Expression; use yii\behaviors\TimestampBehavior; /** * This is the model class for table "websites_pages_has_websites_photos". * * @property integer $id_page * @property integer $id_photo * @property integer $photo_order * @property string $created_at * @property string $updated_at * * @property WebsitesPages $page * @property WebsitesPhotos $photo */ class WebsitesPagesHasWebsitesPhotos extends \yii\db\ActiveRecord { /** * @inheritdoc */ public static function tableName() { return 'websites_pages_has_websites_photos'; } /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'value' => new Expression('LOCALTIMESTAMP'), ] ]; } /** * @inheritdoc */ public function scenarios() { $scenarios = parent::scenarios(); $scenarios['create'] = ['!id_page', '!id_photo', 'photo_order']; $scenarios['update'] = ['photo_order']; return $scenarios; } /** * @inheritdoc */ public function rules() { return [ ['id_page', 'required'], ['id_page', 'number', 'integerOnly' => true, 'min' => 1], ['id_page', 'exist', 'targetClass' => WebsitesPages::className(), 'targetAttribute' => 'id_page'], ['id_photo', 'required'], ['id_photo', 'number', 'integerOnly' => true, 'min' => 1], ['id_photo', 'exist', 'targetClass' => WebsitesPhotos::className(), 'targetAttribute' => 'id_photo'], ['photo_order', 'default', 'value' => 1], ['photo_order', 'required'], ['photo_order', 'number', 'integerOnly' => true, 'min' => 1], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id_page' => Yii::t('common-models', 'ID podstrony'), 'id_photo' => Yii::t('common-models', 'ID zdjęcia'), 'photo_order' => Yii::t('common-models', 'Pozycja'), 'created_at' => Yii::t('common-models', 'Data utworzenia'), 'updated_at' => Yii::t('common-models', 'Data aktualizacji'), ]; } /** * @inheritdoc */ public function beforeDelete() { $flag = true; $models = self::find()->andWhere(['>', 'photo_order', $this->photo_order]) ->andWhere(['id_page' => $this->id_page]) ->andWhere(['!=', 'id_photo', $this->id_photo]) ->all(); if(!empty($models)) { foreach($models as $model) { $model->photo_order -= 1; if(!$model->save(false)) { $flag = false; break; } } } if($flag) { return parent::beforeDelete(); } else { return false; } } /** * @return \yii\db\ActiveQuery */ public function getPage() { return $this->hasOne(WebsitesPages::className(), ['id_page' => 'id_page']); } /** * @return \yii\db\ActiveQuery */ public function getPhoto() { return $this->hasOne(WebsitesPhotos::className(), ['id_photo' => 'id_photo']); } }