【问题标题】:CakePHP data not saving and validation not workingCakePHP 数据未保存且验证不起作用
【发布时间】:2012-05-30 15:37:24
【问题描述】:

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

  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表中,然后将发票保存到invoice表中,否则抛出消息。

【问题讨论】:

    标签: validation cakephp


    【解决方案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-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多