验证总是回归虚假

时间:2012-05-29 15:58:42

标签: validation cakephp

为什么每次我尝试在数据库中输入两个名称时,我的模型都会始终如一地返回false。这是我的整个模型。我正在做的就是检查关系表中是否存在/ biller,partytwo / partyone。当我检查调试工具包时,它显示表单正在接收正确的变量,但是当我检查这个 - > 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;
    }

}

2 个答案:

答案 0 :(得分:1)

你的发现会做有趣的事情......

$relationshipExists=$this->Relationship->find('count', array(
  'conditions' => array(
    'Relationship.partyone',
    'Relationship.partytwo' => $check
  )
));

这可能会产生类似于

的东西
SELECT COUNT(*) FROM relationships as Relationship 
  WHERE Relationship.partyone = 0
  AND Relationship.partytwo = Array;

这是因为你没有传递给Relationship.partyone和一个数组到partytwo。您需要将其更改为以下内容:

$relationshipExists=$this->Relationship->find('count', array(
  'conditions' => array(
    'Relationship.partyone <>' => null,
    'Relationship.partytwo' => current($check) // get the value from the passed var
  )
));

哪会产生类似

的东西
SELECT COUNT(*) FROM relationships as Relationship 
  WHERE Relationship.partyone IS NOT NULL
  AND Relationship.partytwo = 'fieldvalue';

调试$check以了解发送到验证消息的值,并调试查找结果以查看结果。另外,查看SQL日志以查看它生成的SQL。这可以让您更好地了解正在发生的事情。

答案 1 :(得分:0)

您使用的是哪种版本的CakePHP?

无论如何,您不需要&#34; public $ useTable =&#39;发票&#39;;&#34;,因为您的表名称是模型名称复数。 您也不需要&#34; public $ primaryKey =&#39; id&#39;;&#34;,因为它的ID&#39; id&#39;默认情况下。

check this link