1

当我的模型去验证我的表单时

  1. 它总是假的,
  2. 它不保存在数据库中。

我不明白为什么这不起作用,它一直有效,直到我解除了我的一些功能的绑定。

这是我的发票模型,它应该检查relationships_users 表(关系模型)中是否有to/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.');
             }
    }

它应该做的是检查 to/biller 是否在关系用户表中,然后将发票保存到发票表中,否则会抛出消息。

4

1 回答 1

1

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

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

这将搜索与两者的关系partyone partytwo设置为to。您可能想检查它们中的任何一个是否设置为to

        'conditions' => array(
            'OR' => array(
                'Relationship.partyone' => current($check),
                'Relationship.partytwo' => current($check)
            )
        // get the value from the passed var
        )
于 2012-05-30T07:37:24.557 回答