我有以下数据库模型。
Model1 hasMany Users
Model2 hasMany Users
Model3 hasMany Users
一个用户只能属于一个模型。因此,表用户字段如下:
id username password model1_id model2_id model3_id
我需要在 CakePHP 用户模型中检查三个外键不能同时为 NULL。
解决这个问题的最佳方法是什么?由于我无法创建任何自定义验证规则,因此认为验证规则用于验证一个字段。那么我怎样才能验证多个字段并打印相关的验证错误。
you should add this in your Model.
public $validate = array(
'fieldName1' => array(
'rule' => 'ruleName',
'required' => true,
'allowEmpty' => false,
'message' => 'Your Error Message'
)
);
蛋糕验证链接
使用自定义验证
public $validate = array(
'table1_id' => array(
'rule' => array('custom_validation'),
'required' => true,
'allowEmpty' => false,
'message' => 'Your Error Message'
),
'table2_id' => array(
'rule' => array('custom_validation'),
'required' => true,
'allowEmpty' => false,
'message' => 'Your Error Message'
),
'table2_id' => array(
'rule' => array('custom_validation'),
'required' => true,
'allowEmpty' => false,
'message' => 'Your Error Message'
)
);
function custom_validation(){
if( empty($this->data['table1_id']) && empty($this->data['table2_id']) && empty($this->data['table3_id']) ) {
return false;
}
return true;
}