why is my model taking it consistently to return false everytime I try input two names into my database. here is my entire model. All I’m doing is checking to see that both to/biller, partytwo/partyone exist in the relationship table. when i check debug kit, it shows that the form is taking in the correct variables but when I check this->validationErrors – it comes back as nothing? please help.
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;
}}
-relationship model
<?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;
}
}
Your find is going to do interesting things…
This would probably produce something similar to
This is because you’re passing nothing to Relationship.partyone and an array to partytwo. You’ll need to change it to something like this:
Which would produce something like
Debug
$checkto understand what value is being sent to your validation message, and debug the find results to see the results. Also, look at the SQL log to see the SQL it generated. This should give you a better understanding of what’s happening.