繁体   English   中英

cakePHP为空/空字段验证existIn(),如何摆脱……?

[英]cakePHP validating existsIn() for empty/Null fields, how to get rid of…?

我有2个表OrdersProducts orders表中具有外键product_id 有时,订单中不一定必须包含产品(允许没有任何产品ID的订单)。 在这种情况下,我的existIn验证规则会导致问题保存数据。

$rules->add($rules->existsIn(['product_id'], 'Products'));  //Validation in the model of Orders.

注意:请记住,我在数据库中允许product_id为null。

编辑:如果ndn在数据库中可为空,则ndm的注释将根据nullInsingIn的存在检查null或现存的in为默认功能。 如果它不起作用,则可能是您意外地传递了某种值,或者您的列没有被列为可为空。

您还应该能够覆盖其中的存在,以允许出现这种情况或其他情况。 基本上,您将指定一个存在于或它是某个值。 在这种情况下为null。

答案从“ 如何建立规则”修改为cakephp 3中存在或等于一个数字?

$rules->add(
function ($entity, $options) {
    $rule = new ExistsIn(['product_id'], 'Products');
    return $entity->product_id === NULL || $rule($entity, $options);
},
    ['errorField' => 'product_id', 'message' => 'Product ID specified but does not exist']
);

暂无
暂无

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

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