CakePHP - 保存失败; validationErrors为空

时间:2013-11-19 13:27:52

标签: cakephp save

所以,我有这个方法,我保存模型及其相关模型。它具有验证规则,但不会导致导致错误的字段。

传递给save方法的数据没问题,使用atomic = false的保存方法在除了两个之外的每个字段上都返回true,没有任何验证。

saveAll / saveAssociated失败,$this->Model->validationErrors的返回是未被验证为空数组的字段列表。

我已经在网上搜索了这个,并在StackOverflow上找到了类似的问题,但答案并没有解决我的问题。

修改
与我的问题相似的链接:CakePHP save failing with validation errors set but empty

编辑2
这是代码。其余的模型都没问题,所以我不会在这里发布。

**Add Method**

public function add() {
    if ($this->request->is('post')) {
        if(empty($this->request->data['User']['referer'])) {
            $this->request->data['Partner']['status'] = 1;
            $this->request->data['User']['group_id'] = 2;
            $this->request->data['User']['status'] = 1;
            $this->request->data['Customer']['birthday'] = implode("-", array_reverse(explode("/", $this->request->data['Customer']['birthday'])));
            $this->request->data['Customer']['newsletter'] = ($this->request->data['Customer']['newsletter'] == "1") ? true : false;
            $this->request->data['Customer']['accept'] = ($this->request->data['Customer']['accept'] == "1") ? true : false;

            if(!$this->request->data['Customer']['accept']) {
                $this->Session->setFlash(__('You must accept the terms and conditions to proceed.'), 'danger');
            } else {
                $this->Customer->Partner->create();
                var_dump($this->Customer->Partner->saveAssociated($this->request->data, array('atomic' => false)));
                debug($this->Customer->Partner->validationErrors);
                exit();
                if($this->Partner->saveAll($this->request->data)) {
                    $this->Session->setFlash(__('Your profile has been saved. Welcome to Abaixo da Tabela.'), 'success');
                    $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home', 'admin' => false));
                } else {
                    $this->Session->setFlash('Ocorreu um erro ao tentar salvar seu perfil. Verifique se os campos estão preenchidos corretamente e tenta novamente.', 'danger');
                }
            }
        }
    }
}

** Partner Model **
class Partner extends AppModel {
    public $validate = array(
        'person_type' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'status' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
    );

    public $hasMany = array(
        'Address' => array(
            'className' => 'Address',
            'foreignKey' => 'partner_id',
        ),
        'Contact' => array(
            'className' => 'Contact',
            'foreignKey' => 'partner_id',
        ),
        'Customer' => array(
            'className' => 'Customer',
            'foreignKey' => 'partner_id',
        ),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'partner_id',
        ),
    );
}

** Address Model **
class Address extends AppModel {
    public $validate = array(
        'partner_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'address' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'number' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'zip' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'district' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'city_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'uf' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
    );

    public $belongsTo = array(
        'Partner' => array(
            'className' => 'Partner',
            'foreignKey' => 'partner_id',
        ),
        'City' => array(
            'className' => 'City',
            'foreignKey' => 'city_id',
        )
    );
}

** Customer Model **
class Customer extends AppModel {
    public $validate = array(
        'partner_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'birthday' => array(
            'date' => array(
                'rule' => array('date'),
            ),
        ),
    );

    public $belongsTo = array(
        'Partner' => array(
            'className' => 'Partner',
            'foreignKey' => 'partner_id',
        ),
        'City' => array(
            'className' => 'City',
            'foreignKey' => 'city_id',
        )
    );
}

** debug($this->Customer->Partner->validationErrors) **
array (size=5)
'Partner' => boolean true
'Address' => 
    array (size=7)
        'zip' => boolean true
        'address' => boolean true
        'number' => boolean true
        'adjunct' => boolean false
        'district' => boolean true
        'uf' => boolean true
        'city_id' => boolean true
'Contact' => 
    array (size=3)
        'email' => boolean true
        'phone_1' => boolean true
        'phone_2' => boolean true
'User' => 
    array (size=5)
        'username' => boolean true
        'password' => boolean true
        'password_confirm' => boolean true
        'group_id' => boolean true
        'status' => boolean true
'Customer' => 
    array (size=4)
        'genre' => boolean true
        'birthday' => boolean true
        'newsletter' => boolean false
        'accept' => boolean true

注意save方法返回时的两个错误响应。

1 个答案:

答案 0 :(得分:0)

(通过问题编辑回答。转换为社区维基回答。请参阅Question with no answers, but issue solved in the comments (or extended in chat)

OP写道:

  

原来,错误发生在视图中。相关模型的表单字段如下:

echo $this->Form->input('Customer.birthday')
echo $this->Form->input('Customer.newsletter')
echo $this->Form->input('Customer.accept')
  

正确的方法是:

echo $this->Form->input('Customer.0.birthday')
echo $this->Form->input('Customer.0.newsletter')
echo $this->Form->input('Customer.0.accept')
  

与本书一样,具有hasMany关联的模型的字段需要索引,在这种情况下,需要0