CakePHP数据未保存且验证无效

时间:2012-05-30 06:23:10

标签: validation cakephp

当我的模型验证我的表格时

  1. 总是假的,
  2. 它不会保存在数据库中。
  3. 我不明白为什么这不起作用,它一直在工作,直到我解除了我的一些功能。

    这是我的发票模型,它应该检查在relationship_users表(关系模型)中是否有/ biller。

    <?php
    
    class Invoice extends AppModel{ 
        var $name='Invoice'; 
        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' => current($check),
                    'Relationship.partytwo' => current($check)
                // get the value from the passed var
                )
            )
            );
                if ($relationshipExists>0) {
                    return TRUE;
                }
                else
                    return FALSE;
        }
    

    这是我在发票表中的功能

     public function addinvoice(){
        $this->set('title_for_layout', 'Create Invoice');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.jpg');   
        $this->layout='home_layout';
        if($this->request->is('post')){
            ($this->Invoice->set($this->request->data));
            if($this->Invoice->validates(array('fieldList'=>array('to','Invoice.relationshipExists')))){
                $this->Invoice->save($this->request->data); 
                $this->Session->setFlash('The invoice has been saved');  
          }}else { 
                    $this->Session->setFlash('The invoice could not be saved. Please, try again.');
                 }
        }
    

    它应该做的是检查/ biller是否在relations_users表中,然后将发票保存到invoice表中,否则抛出一条消息。

1 个答案:

答案 0 :(得分:1)

条件数组对我来说很奇怪:

        'conditions' => array(
            'Relationship.partyone' => current($check),
            'Relationship.partytwo' => current($check)
        // get the value from the passed var
        )

这将搜索设置为partyone的{​​{1}} partytwo的关系。您可能想要检查 中的是否设置为to

to