0

saveAll 不起作用可以有人帮我找出问题所在。我已经包含了控制器的操作、传递给 saveAll 的数组及其模型。

包含 saveAll 的控制器操作如下所示 -

public function add_customer_order() {
    if ($this->request->is('post')) {
                      -
                      -
                      -
                      -
                   $this->CustomerOrderItem->create();
                   if ($this->CustomerOrderItem->saveAll($customer_order_item)) {
                        $this->Session->setFlash('The customer order Items has been saved successfully.', 'default/flash_success');
                     }
                else {
                    $this->Session->setFlash('The customer order Items not been saved successfully.', 'default/flash_success');
                }
    }

}

数组 $customer_order_item 如下所示 -

array(
    'CustomerOrderItem' => array(
        (int) 0 => array(
            'id' => '',
            'quantity' => '400',
            'unit_cost' => '77',
            'pending_quantity' => '400',
            'packaging_configuration_quantity' => '20',
            'exercise_duty_id' => '0',
            'tax_id' => '5',
            'customer_order_id' => '',
            'master_article_id' => '22'
        ),
        (int) 1 => array(
            'id' => '',
            'quantity' => '200',
            'unit_cost' => '77',
            'pending_quantity' => '200',
            'packaging_configuration_quantity' => '20',
            'exercise_duty_id' => '0',
            'tax_id' => '5',
            'customer_order_id' => '',
            'master_article_id' => '25'
        ),
        (int) 2 => array(
            'id' => '',
            'quantity' => '400',
            'unit_cost' => '77',
            'pending_quantity' => '400',
            'packaging_configuration_quantity' => '20',
            'exercise_duty_id' => '1',
            'tax_id' => '5',
            'customer_order_id' => '',
            'master_article_id' => '23'
        ),
        (int) 3 => array(
            'id' => '',
            'quantity' => '200',
            'unit_cost' => '77',
            'pending_quantity' => '200',
            'packaging_configuration_quantity' => '20',
            'exercise_duty_id' => '1',
            'tax_id' => '5',
            'customer_order_id' => '',
            'master_article_id' => '24'
        ),
        (int) 4 => array(
            'id' => '',
            'quantity' => '200',
            'unit_cost' => '77',
            'pending_quantity' => '200',
            'packaging_configuration_quantity' => '20',
            'exercise_duty_id' => '1',
            'tax_id' => '5',
            'customer_order_id' => '',
            'master_article_id' => '27'
        )
    )
)

该模型如下所示 -

<?php
App::uses('AppModel', 'Model');
/**
 * CustomerOrderItem Model
 *
 * @property CustomerOrder $CustomerOrder
 * @property MasterArticle $MasterArticle
 */
class CustomerOrderItem extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'customer_order_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'master_article_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'CustomerOrder' => array(
            'className' => 'CustomerOrder',
            'foreignKey' => 'customer_order_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'MasterArticle' => array(
            'className' => 'MasterArticle',
            'foreignKey' => 'master_article_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}
4

3 回答 3

1

我很确定这是由于验证或数据库中的空值导致的数据问题。

  1. 调试内容debug($this->request->data);
  2. 调试无效字段(如果有debug($this->Submission->invalidFields());)。确保在您有问题validates()请求或saveAll()
于 2013-03-10T11:44:15.540 回答
0

First of all remove all the validation rules and then check again if error is still coming.if not then there must be issue with data validation rules or null value of 'customer_order_id'.

于 2013-08-02T13:31:03.970 回答
0

尝试

if ($this->CustomerOrderItem->saveAll($this->request->data)) {
于 2013-03-09T06:44:25.207 回答