Getting unknown property Exception in yii2

15,328

Because this field is not present in the @property comment I guess you have added it after the model has been generated. If you have got the DB schema cached new fields are not fetched until cache is updated. Try to remove the cache for DB.

Share:
15,328

Related videos on Youtube

Behzad Hassani
Author by

Behzad Hassani

Web Programmer and Developer Web Template Designer

Updated on June 04, 2022

Comments

  • Behzad Hassani
    Behzad Hassani almost 2 years

    here I have a model in yii2

    <?php
    
    namespace app\models;
    
    /**
     * This is the model class for table "car_ad".
     *
     * @property integer $id
     * @property integer $brand_id
     * @property integer $sub_brand_id
     * @property integer $sell_type
     * @property integer $year
     * @property integer $the_function
     * @property integer $fuel_type_id
     * @property integer $gearbox
     * @property integer $sell_mode
     * @property integer $real_price
     * @property integer $prepayment
     * @property integer $installment_price
     * @property integer $no_installments
     * @property integer $delivery_time_id
     * @property integer $installments_period
     * @property integer $body_status_id
     * @property integer $body_color_id
     * @property integer $inside_color_id
     * @property integer $number_type
     * @property string $description
     * @property integer $ad_type_id
     * @property integer $provice_id
     * @property integer $city_id
     * @property string $address
     * @property string $lang
     * @property string $lat
     * @property string $creation_date
     * @property integer $user_id
     */
    class CarAd extends \yii\db\ActiveRecord
    {
        public $imageFiles;
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'car_ad';
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['brand_id', 'sub_brand_id', 'sell_type', 'year', 'used_value', 'fuel_type_id', 'gearbox', 'body_status_id', 'body_color_id', 'number_type', 'ad_type_id', 'provice_id', 'city_id', 'address', 'lang', 'lat', 'creation_date', 'user_id'], 'required'],
                [['brand_id', 'sub_brand_id', 'sell_type', 'year', 'fuel_type_id', 'used_value ', 'gearbox', 'sell_mode', 'real_price', 'prepayment', 'installment_price', 'no_installments', 'delivery_time_id', 'installments_period', 'body_status_id', 'body_color_id', 'inside_color_id', 'number_type', 'ad_type_id', 'provice_id', 'city_id', 'creation_date', 'user_id'], 'integer'],            
                [['description'], 'string'],
                [['address', 'lang', 'lat'], 'string', 'max' => 512],
                [['imageFiles'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg', 'maxFiles' => 10],
            ];
        }
    
        public function upload()
        {   
    
            foreach ($this->imageFiles as $file) {
                $image = New CarAdImage();            
                $image->image = $file->baseName . '.' . $file->extension;
                $image->car_ad_id = $this->id;
                $image->save();
                $file->saveAs('img/car_ad/' . $file->baseName . '.' . $file->extension);
            }        
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'id' => 'شناسه', 'brand_id' => 'برند', 'sub_brand_id' => 'مدل','sell_type' => 'فروش به صورت',
                'year' => 'سال','used_value' => 'کارکرد','fuel_type_id' => 'سیستم سوخت','gearbox' => 'گیربکس',
                'sell_mode' => 'نوع فروش','real_price' => 'قیمت نقدی','prepayment' => 'پیش پرداخت','installment_price' => 'مبلغ هر قسط','no_installments' => 'تعداد اقساط','delivery_time_id' => 'موعد تحویل',
                'installments_period' => 'دوره پرداخت',
                'body_status_id' => 'وضعیت بدنه',
                'body_color_id' => 'رنگ بدنه',
                'inside_color_id' => 'رنگ داخل',
                'number_type' => 'نوع پلاک',
                'description' => 'توضیحات اضافی',
                'ad_type_id' => 'نوع آگهی',
                'provice_id' => 'استان',
                'city_id' => 'شهرستان',
                'address' => 'آدرس',
                'lang' => 'طول جغرافیایی',
                'lat' => 'عرض جغرافیایی',
                'creation_date' => 'تاریخ ایجاد',
                'user_id' => 'کاربر ایجاد کننده',
                'imageFiles' => 'تصاویر آگهی'
            ];
        }
    }
    

    when I want to submit the form I face with this error.

    Getting unknown property: app\models\CarAd::used_value

    but as you see I have this field in my fields. My table name is car_ad. what is the problem with my code?

  • Behzad Hassani
    Behzad Hassani almost 8 years
    I hav changed the field in the database, first it's name was the_function and I have got same error then I renamed it and nothing changed
  • Bizley
    Bizley almost 8 years
    So there is no 'enableSchemaCache' => true in your DB connection config?
  • Behzad Hassani
    Behzad Hassani almost 8 years
    no there isn;t. I have refresh the schema in my action too :(
  • Bizley
    Bizley almost 8 years
    I've noticed there is space at the end of this name in the second row of rules. Maybe this is the cause?
  • K Arun Singh
    K Arun Singh about 6 years
    You can execute Yii::$app->cache->flush(); in any controller to clear cache, It worked for me
  • Simas Joneliunas
    Simas Joneliunas over 2 years
    Please don't add "thank you" as an answer. Instead, vote up the answers that you find helpful. - From Review
  • stakavi
    stakavi over 2 years
    I got it, but I can't to vote, because I have not enaugh reputation