简体   繁体   English

Yii2中的外键验证规则

[英]Foreign key validation rule in Yii2

I have parent_id that can have NULL value - default sets to 0 . 我有parent_id可以具有NULL值-默认设置为0

Model 模型

...
[['parent_id'], 'integer'],
[['parent_id'], 'default', 'value' => 0],
[['parent_id'], 'exist', 'targetAttribute' => 'id', 'skipOnEmpty' => true],
...

But exist rule didn't work. 但是存在规则不起作用。

What I'm doing wrong? 我做错了什么?

UPDATE 更新

Today I removed the following rule and it works: 今天,我删除了以下规则,它起作用了:

...
[['parent_id'], 'default', 'value' => 0],
...

But what do I do if want to change the default to other value? 但是,如果要将默认值更改为其他值怎么办?

change the order of rules: 更改规则顺序:

[['parent_id'], 'integer'],
[['parent_id'], 'exist', 'targetAttribute' => 'id', 'skipOnEmpty' => true],
[['parent_id'], 'default', 'value' => 0],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM