0

I am trying to insert an item into my database using Yii, but I am getting the following error:

Unknown Property – yii\base\UnknownPropertyException

Getting unknown property: app\models\Item::lock

in .../_protected/vendor/yiisoft/yii2/base/Component.php

I am a complete beginner in Yii and PHP, so I don't even know where to look. I have tried finding something similar online, and found that the potential cause could be the case-sensitivity: my model class is called Item, and my table is called item (phpMyAdmin changed the name to lowercase), but I still have no idea what to do.

Edit:

This is my Item model:

class Item extends BaseItem
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return array_replace_recursive(parent::rules(),
        [
            [['InventoryNumber', 'ItemStatus', 'ItemType', 'ItemName', 'PurchaseDate', 'PurchaseValue', 'Amortization', 'LocationId', 'PersonId'], 'required'],
            [['InventoryNumber', 'LocationId', 'PersonId'], 'integer'],
            [['PurchaseDate'], 'safe'],
            [['PurchaseValue', 'Amortization'], 'number'],
            [['ItemStatus'], 'string', 'max' => 20],
            [['ItemType'], 'string', 'max' => 30],
            [['ItemName'], 'string', 'max' => 100],
            [['InventoryNumber'], 'unique'],
            [['lock'], 'default', 'value' => '0'],
            [['lock'], 'mootensai\components\OptimisticLockValidator']
        ]);
    }   
}

The lock fields are automatically generated, I haven't added them into my database.

3
  • Does your database table has lock field? Commented Jun 22, 2017 at 10:22
  • @paul No, it doesn't, that was automatically generated. Commented Jun 22, 2017 at 10:23
  • Then you might declare lock as safe. [['PurchaseDate', 'lock'], 'safe'], Commented Jun 22, 2017 at 10:26

1 Answer 1

2

Add declaration to your class

class Item extends BaseItem
{
    public $lock;
.....
}
Sign up to request clarification or add additional context in comments.

2 Comments

Now I'm getting Setting unknown property: app\models\Item::created_at
So continue the same way. If you have not fields in DB table, you should add property to the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.