为什么我的模型每次尝试在数据库中输入两个名称时都始终返回 false。这是我的整个模型。我所做的只是检查关系表中是否存在 to/biller、partytwo/partyone。当我检查调试工具包时,它显示表单正在接受正确的变量,但是当我检查 this->validationErrors - 它什么都没有返回?请帮忙。
class Invoice extends AppModel{
var $name='Invoice';
public $useTable = 'invoices';
public $primaryKey = 'id';
public $belongsTo = array(
'Relationship' =>array(
'className' => 'Relationship',
'foreignKey' =>'relationship_id',
)
);
var $validate = array(
'to' => array(
'relationshipExists' => array(
'rule' => array(
'relationshipExists',
),
'message' => 'sorry you dont have a relationship with that user.'
),
),
);
public function relationshipExists($check){
$relationshipExists=$this->Relationship->find('count', array('conditions' => array('Relationship.partyone','Relationship.partytwo'=>$check)));
if ($relationshipExists == true) {
return TRUE;
}
else
return FALSE;
}}
-关系模型
<?php
class Relationship extends AppModel
{
var $name = 'Relationship';
public $useTable = 'relationships_users';
public $hasMany = array(
'Invoice' =>
array(
'className' => 'Invoice',
'joinTable' => 'invoice',
'foreignKey' => 'invoice_id'));
public $belongsTo = array(
'User' =>array(
'className' => 'User',
'foreignKey' =>'partyone','partytwo',
'associationForeignKey' => 'username',
));
var $validate = array(
'date' => array(
'rule' => array(
'datevalidation',
'systemDate'
),
'message' => 'Current Date and System Date is mismatched'
),
'partytwo' => array(
'userExists' => array(
'rule' => array(
'userExists',
),
'message' => 'That username doesnt exist.'
),
),
);
function datevalidation($field = array(), $compare_field = null)
{
if ($field['date'] > $compare_field)
return TRUE;
else
return FALSE;
}
function userExists($check)
{
$userExists = $this->User->find('count', array('conditions' => array('User.username'=>$check)));
if ($userExists == 1) {
return TRUE;
}
else
return FALSE;
}
}