Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace backend\models;
- use \yii\helpers\ArrayHelper;
- use yii\helpers\Url;
- use Yii;
- /**
- * This is the model class for table "images_manager".
- *
- * @property int $id
- * @property string $name
- * @property string $class
- * @property int $product_id
- * @property string $alt
- * @property string $sort
- */
- class ImagesManager extends \yii\db\ActiveRecord
- {
- public $attachment;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'images_manager';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['product_id'], 'required'],
- [['product_id', 'sort'], 'integer'],
- [['name', 'class'], 'string', 'max' => 250],
- [['alt'], 'string', 'max' => 50],
- [['attachment'], 'image'],
- [['sort'], 'default', 'value'=>function($model){
- $count = ImagesManager::find()->andWhere
- (['class'=>$model->class])->count();
- return ($count > 0)?$count++:0;
- }]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'name' => 'Name',
- 'class' => 'Class',
- 'product_id' => 'Product ID',
- 'alt' => 'Alt',
- ];
- }
- public function getImageUrl()
- {
- if($this->name){
- $path = 'http://localhost/mobile-go/uploads/images/'.$this->name;
- }else{
- $path = str_replace('admin.','',Url::home(true)).'uploads/images/nophoto.svg';
- }
- return $path;
- }
- public function beforeDelete(){
- if (parent::beforeDelete()) {
- ImagesManager::updateAllCounters(['sort' => -1], ['and', ['class' => 'Product', 'product_id' =>$this->product_id],['>','sort',$this->sort]]);
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement