0

为什么我的模型每次尝试在数据库中输入两个名称时都始终返回 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;
    }

}
4

2 回答 2

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。这应该让您更好地了解正在发生的事情。

于 2012-05-29T16:21:27.100 回答
0

您使用的是什么版本的 CakePHP?

无论如何,您不需要“public $useTable = 'invoices';”,因为您的表格名称是复数的模型名称。您也不需要“public $primaryKey = 'id';”,因为默认情况下它是 'id'。

检查这个链接

于 2012-05-29T16:04:06.027 回答