【发布时间】:2017-06-22 10:05:21
【问题描述】:
我正在尝试使用 Yii 将项目插入到我的数据库中,但出现以下错误:
未知属性 – yii\base\UnknownPropertyException
获取未知属性: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 字段是自动生成的,我还没有将它们添加到我的数据库中。
【问题讨论】:
-
你的数据库表有
lock字段吗? -
@paul 不,不是,它是自动生成的。
-
那么您可以将
lock声明为safe。[['PurchaseDate', 'lock'], 'safe'],