我尝试使用 Yii 将项目插入到我的数据库中,但出现以下错误:
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: app\models\Item::lock
在.../_protected/vendor/yiisoft/yii2/base/Component.php
我是 Yii 和 PHP 的初学者,所以我什至不知道该去哪里寻找。我尝试在网上找到类似的东西,发现潜在的原因可能是区分大小写:我的模型类称为 Item
,我的表称为 item
(phpMyAdmin将名称更改为小写),但我仍然不知道该怎么办。
编辑:
这是我的Item
模型:
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']
]);
}
}
lock
字段是自动生成的,我还没有将它们添加到我的数据库中。
最佳答案
向您的类添加声明
class Item extends BaseItem
{
public $lock;
.....
}
关于PHP、Yii - 未知属性异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44696255/