CakePHP patchEntity不保存数据(“通过”关联)

时间:2016-11-23 07:28:14

标签: cakephp orm associations has-and-belongs-to-many

我是CakePHP的新手,并且一直在使用CakePHP cookbook和stackoverflow来尝试解决我的问题,但却无法解决。这可能是愚蠢的事。

我有引号,项目和joindata QuotesItems的连接表。

我在edit.ctp(模板引号)中有这个:

<div class="quotes form large-9 medium-8 columns content">
    <?= $this->Form->create($quote) ?>
    <fieldset>
        <legend><?= __('Edit Quote') ?></legend>
        <?php
            echo $this->Form->input('customer_id', ['options' => $customers]);
            echo $this->Form->input('items._ids', ['options' => $items]);
        $counter = 0;
        foreach($quote['items'] as $item):
            echo $this->Form->input( "items.$counter.description", array( 'disabled' => 'disabled' ) );
            echo $this->Form->input("items.$counter._joinData.item_quantity");
            $counter++;
        endforeach
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

在QuotesController中我有编辑功能:

    public function edit($id = null)
    {
        $quote = $this->Quotes->get($id, [
            'contain' => ['Items']
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $quote = $this->Quotes->patchEntity($quote, $this->request->data, ['Quotes._joinData']);
            //$quote = $this->Quotes->patchEntity($quote, $this->request->data, ['associated'=>['QuotesItems'=>['associated'=>['Items']]]] );
            if ($this->Quotes->save($quote)) {
debug($this->request->data);
                $this->Flash->success(__('The quote has been saved.'));

                //return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The quote could not be saved. Please, try again.'));
            }
        }
        $customers = $this->Quotes->Customers->find('list', ['limit' => 200]);
        $items = $this->Quotes->Items->find('list', ['limit' => 200]);
        $this->set(compact('quote', 'customers', 'items'));
        $this->set('_serialize', ['quote']);
    }

保存表单时发送的数组是:

[
    'customer_id' => '1',
    'items' => [
        '_ids' => [
            (int) 0 => '1'
        ],
        (int) 0 => [
            '_joinData' => [
                'item_quantity' => '18'
            ]
        ]
    ]
]

我已经检查了食谱的保存“保存数据”,但它没有弄清楚我做错了什么。

数据是否需要报价的ID?我正在迭代这些项目的方式是正确的吗?

表关联如下: 引号:

    $this->belongsToMany('Items', [
        'through' => 'QuotesItems'
    ]);

产品:

    $this->belongsToMany('Quotes', [
        'through' => 'QuotesItems'
    ]);

QuotesItems(jointable):

    $this->belongsTo('Quotes', [
        'foreignKey' => 'quote_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Items', [
        'foreignKey' => 'item_id',
        'joinType' => 'INNER'
    ]);

提前致谢!

1 个答案:

答案 0 :(得分:0)

尝试制作类似这样的数组格式:

[
 'customer_id' => '1',
 'items' => [
    'id' => 1, // this is primary key of items table
    'item_quantity' => '18' // this is another field of items table
    ]
]

在编辑方法中使用注释行:

$quote = $this->Quotes->patchEntity($quote, $this->request->data, ['associated'=>['QuotesItems'=>['associated'=>['Items']]]] );
相关问题