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がrelationships_usersテーブルにあることを確認してから、請求書を請求書テーブルに保存する必要があります。そうでない場合は、メッセージをスローします。

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 に答える