رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
امنیت در آپدیت کردن یک رکورد (حل شد)
#1
سلام . این کد های مدل من :
<?php

namespace backendmodels;

use commonComponentfunctions;
use Yii;
use yiihelpersArrayHelper;

/**
* This is the model class for table "{{%end_users}}".
*
* @property string $detail_id
* @property integer $user_id
* @property string $mobile_number
* @property string $name
* @property string $family
* @property integer $birth_date
* @property integer $gender
* @property string $profile_picture
* @property string $meli_code
* @property string $meli_picture
* @property integer $meli_picture_authorized
* @property integer $phone_number_authorized
* @property integer $email_authorized
* @property integer $city_id
* @property string $address
* @property string $postal_code
* @property double $lat
* @property double $lng
* @property double $stock_account
* @property integer $date_added
* @property integer $last_update
* @property integer $enabled
*
* @property User $user
* @property Locate $city
* @property Factors[] $factors
* @property Shops[] $shops
*/
class EndUsers extends yiidbActiveRecord
{
   public $meli_image;
   public $profile_image;


   //scenario ha
   const Scenario_Update_Profile='profile';
   const Scenario_Update_Personal_Information='personalinformation';
   const Scenario_Update_Mobile='mobile';
   const Scenario_Update_Meli='meli';
   //
   private $_oldAttributes;

   /**
    * @inheritdoc
    */
   public static function tableName()
   {
       return '{{%end_users}}';
   }

   /**
    * @inheritdoc
    */
   public function rules()
   {
       return [
           [['user_id', 'mobile_number', 'date_added', 'last_update'], 'required'],
           [['user_id', 'birth_date', 'gender', 'meli_picture_authorized', 'phone_number_authorized', 'email_authorized', 'city_id', 'date_added', 'last_update', 'enabled'], 'integer'],
           [['lat', 'lng', 'stock_account'], 'number'],
           [['mobile_number', 'meli_code', 'postal_code'], 'string', 'max' => 15],
           [['name'], 'string', 'max' => 30],
           [['family'], 'string', 'max' => 50],
           [['profile_picture', 'meli_picture'], 'string', 'max' => 100],
           [['address'], 'string', 'max' => 500],
           [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
           [['city_id'], 'exist', 'skipOnError' => true, 'targetClass' => Locate::className(), 'targetAttribute' => ['city_id' => 'idLocate']],
           [['meli_image'], 'safe'],
           [['meli_image'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
           [['profile_image'], 'safe'],
           [['profile_image'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
       ];
   }

   /**
    * @inheritdoc
    */
   public function attributeLabels()
   {
       return [
            'detail_id' => Yii::t('app', 'Detail ID'),
            'user_id' => Yii::t('app', 'User ID'),
            'mobile_number' => Yii::t('app', 'شماره موبایل'),
            'name' => Yii::t('app', 'نام'),
            'family' => Yii::t('app', 'نام خانوادگی'),
            'birth_date' => Yii::t('app', 'تاریخ تولد'),
            'gender' => Yii::t('app', 'جنسیت'),
            'profile_picture' => Yii::t('app', 'تصویر پروفایل'),
            'meli_image' => Yii::t('app', 'تصویر ملی'),
            'profile_image' => Yii::t('app', 'تصویر پروفایل'),
            'meli_code' => Yii::t('app', 'کد ملی'),
            'meli_picture' => Yii::t('app', 'تصویر ملی'),
            'meli_picture_authorized' => Yii::t('app', 'Meli Picture Authorized'),
            'phone_number_authorized' => Yii::t('app', 'Phone Number Authorized'),
            'email_authorized' => Yii::t('app', 'Email Authorized'),
            'city_id' => Yii::t('app', 'City ID'),
            'address' => Yii::t('app', 'آدرس'),
            'postal_code' => Yii::t('app', 'کد پستی'),
            'lat' => Yii::t('app', 'Lat'),
            'lng' => Yii::t('app', 'Lng'),
            'stock_account' => Yii::t('app', 'Stock Account'),
            'date_added' => Yii::t('app', 'Date Added'),
            'last_update' => Yii::t('app', 'Last Update'),
            'enabled' => Yii::t('app', 'Enabled'),
        ];
   }

   public function save($runValidation = true, $attributeNames = null)
   {
       if (isset($_POST['birth_date'])){
           $timeStamp =functions::convertDateToTimeStamp(Yii::$app->request->post('birth_date'));
           $this->birth_date = $timeStamp;
       }
       if (isset($_POST['gender'])){
           $this->gender = Yii::$app->request->post('gender');
       }
       if (isset($_POST['city_id'])){
           $this->city_id = Yii::$app->request->post('city_id');
       }
       $this->last_update = time();
       return parent::save($runValidation, $attributeNames); // TODO: Change the autogenerated stub
   }


   public function update($runValidation = true, $attributeNames = null)
   {
       $this->last_update=time();
       return parent::update($runValidation, $attributeNames); // TODO: Change the autogenerated stub
   }
   /**
    * @return yiidbActiveQuery
    */
   public function getUser()
   {
       return $this->hasOne(User::className(), ['id' => 'user_id']);
   }

   /**
    * @return yiidbActiveQuery
    */
   public function getLocate()
   {
       return $this->hasOne(Locate::className(), ['idLocate' => 'city_id']);
   }

   /**
    * @return yiidbActiveQuery
    */
   public function getFactors()
   {
       return $this->hasMany(Factors::className(), ['user_detail_id' => 'detail_id']);
   }

   /**
    * @return yiidbActiveQuery
    */
   public function getShops()
   {
       return $this->hasMany(Shops::className(), ['owner_detail' => 'detail_id']);
   }

   public function uploadMeliImage()
   {
       if ($this->validate()) {
           $this->meli_image->saveAs(Yii::getAlias('@meli2') . '/' . $this->meli_picture);
           return true;
       } else {
           return false;
       }
   }
   public function uploadProfileImage()
   {
       if ($this->validate()) {
           $this->profile_image->saveAs(Yii::getAlias('@profile2') . '/' . $this->profile_picture);
           return true;
       } else {
           return false;
       }
   }

   public function scenarios()
   {
       $scenarios = parent::scenarios();
       $scenarios[self::Scenario_Update_Profile] = ['profile_picture'];
       $scenarios[self::Scenario_Update_Meli] = ['meli_code'];
       $scenarios[self::Scenario_Update_Mobile] = ['mobile_number'];
       $scenarios[self::Scenario_Update_Personal_Information] = ['name'];
       return $scenarios;
   }


   public function afterFind()
   {
       $this->_oldAttributes = $this->attributes;
       parent::afterFind(); // TODO: Change the autogenerated stub
   }

   public function beforeSave($action)
   {
       switch($this->scenario) {
           case self::Scenario_Update_Profile:
               $only = ['detail_id','profile_picture'];
               $attributes = $this->_oldAttributes;
               foreach($only as $field) {
                   $attributes[$field] = $this->attributes[$field];
               }
               $this->attributes = $attributes;
               break;
       }
       return parent::beforeSave($action); // TODO: Change the autogenerated stub
   }

}

میخوام وقتی که مدل رو روی یک سناریو خاص قرار دادم فقط فیلد هایی که مشخص کردم قابلیت آپدیت داشته باشن
پاسخ
تشکر شده توسط:
#2
توی afterFind آخرش خروجی parent::afterFind رو return کنید ببینید درست میشه؟
public function afterFind()
{
    $this->_oldAttributes = $this->attributes;
    return parent::afterFind();
}
پاسخ
تشکر شده توسط: hossein73
#3
با این کد مشکل حل شد:
public function beforeSave($action)
{
    switch($this->scenario) {
        case self::Scenario_Update_Profile:
            $only = ['detail_id','profile_picture'];
            $keys = array_keys($this->attributes);
            foreach($keys as $key) {
                $this->{$key} = (in_array($key, $only) ? $this->attributes[$key] : $this->_oldAttributes[$key]);
            }
            break;
     }
     return parent::beforeSave($action);
}
پاسخ
تشکر شده توسط:




کاربران در حال بازدید این موضوع: 1 مهمان