【问题标题】:CakePHP saveAll() function problemsCakePHP saveAll() 函数问题
【发布时间】:2010-01-28 05:36:28
【问题描述】:

我正在尝试将记录保存到表格以及相关记录。主表称为quotes,它有一个指向quote_items 表的hasmany 链接。报价单属于报价单

当我尝试保存时,它会将记录保存在报价单中,但不会将记录保存在报价单项中 .

下面是我的报价添加功能

    function add() {
    if (!empty($this->data)) {
        $this->Quote->create();
        if ($this->Quote->saveAll($this->data)) {
            $this->Session->setFlash(__('The Quote has been saved', true));
            //$this->redirect(array('action'=>'index'));
        } else {
            $this->Session->setFlash(__('The Quote could not be saved. Please, try again.', true));
        }
    }
    $this->Quote->recursive = 2;
    $statuses = $this->Quote->Status->find('list');
    $contacts = $this->Quote->Contact->find('list');
    $this->set(compact('statuses', 'contacts'));
}

报价视图/表单设置

    <?php echo $form->create('Quote', array('action' => 'add'));?>
    <fieldset>
        <legend><?php __('Add Quote');?></legend>
    <?php
        echo $form->input('Quote.name');
        echo $form->input('Quote.revision');
        echo $form->input('Quote.status_id');
        echo $form->input('Quote.contact_id');
        echo $form->input('quote_item.product_id');
        echo $form->input('quote_item.name');
        echo $form->input('quote_item.price');
        echo $form->input('quote_item.description');
        echo $form->input('Quote.totalcost');
    ?>
    </fieldset>
<?php echo $form->end('Submit');?>

表单提交时返回的数组

    Array ( 
    [quote] => Array ( 
                [name] => Test 
                [revision] => 1 
                [status_id] => 1 
                [contact_id] => 1 
                [totalcost] => 123 
              ) 

    [quote_item] => Array ( 
                [product_id] => 1
                [name] => test 
                [price] => 123 
                [description] => tes 1234 
              ) 
) 

这似乎完全符合 cakephp 文档中列出的内容,所以我无法弄清楚它为什么不起作用 - http://book.cakephp.org/view/84/Saving-Related-Model-Data-hasOne-hasMany-belongsTo

提前致谢

【问题讨论】:

    标签: php mysql database cakephp


    【解决方案1】:

    构建表单的正确方法是:

    echo $form->input('Quote.name');
    ...
    echo $form->input('QuoteItem.0.product_id');
    echo $form->input('QuoteItem.0.name');
    ...
    echo $form->input('QuoteItem.1.product_id');
    echo $form->input('QuoteItem.1.name');
    

    生成的数组应如下所示:

    array(
        'Quote' => array(
            'name' => 'Test'
            ....
        ),
        'QuoteItem' => array(
            0 => array(
                'product_id' => 1
                'name' => 'test'
                ...
            )
            1 => array(
                'product_id' => 2
                'name' => 'test'
                ...
            )
        )
    )
    

    根据命名约定,模型名称是驼峰化的(ModelName,而不是 model_name)。此外,由于 Quote hasMany QuoteItems,QuoteItem 数组需要由许多 QuoteItem 数组组成。希望这是有道理的。 :)

    【讨论】:

    • 意义非凡,当我想修复它时,我离得太近了,但我没有意识到需要摆脱下划线。非常感谢你:D
    猜你喜欢
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 2017-06-18
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多