如何正确地将数据保存在BelongsToMany关联中?

时间:2019-04-26 20:03:48

标签: php cakephp-3.7

我的应用程序管理电子产品车间的维修订单。 当我尝试保存几个相同的项目(具有相同的ID)时,cake仅保留最后一个条目,而忽略先前的条目。 (在我的示例中,最后一个产品)

我试图在模型中配置“ saveStrategy”,并尝试使用“ append”,但是没有任何结果。当我输入不同的产品(具有不同的ID)时,它可以毫无问题地保存它。

表格:

门票

| id | client_id | reference |

产品

| id | code | description |

产品门票

| id | ticket_id | product_id | qty | serial_number | notes |

debug($this->request->getData());

[
    'client_id' => '1',
    'reference' => 'Client reference',
    'products' => [
        (int) 0 => [
            'id' => '1',
            '_joinData' => [
                'qty' => '1',
                'serial_number' => '123',
                'notes' => 'Not working'
            ]
        ],
        (int) 1 => [
            'id' => '1',
            '_joinData' => [
                'qty' => '1',
                'serial_number' => '345',
                'notes' => 'Not Working too'
            ]
        ],
        (int) 2 => [
            'id' => '1',
            '_joinData' => [
                'qty' => '1',
                'serial_number' => '567',
                'notes' => 'Note number 3'
            ]
        ],
        (int) 3 => [
            'id' => '1',
            '_joinData' => [
                'qty' => '1',
                'serial_number' => '978',
                'notes' => 'Another note'
            ]
        ]
    ]
]

它应该保存的是所有数据,而不仅仅是最后一个值。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的业务案例,则说明您正在为客户创建票证,并且在同一笔交易中向票证添加了4种产品。因此,您要添加带有4个关联的product_tickets(票据上的项目)的票据。

在模型ProductsTable.php

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('products');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('ProductTickets', [
            'foreignKey' => 'product_id'
        ]);
    }

在TicketsTable.php模型中

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('tickets');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('ProductTickets', [
            'foreignKey' => 'ticket_id'
        ]);
    }

在模型ProductTicketsTable.php

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('product_tickets');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Tickets', [
            'foreignKey' => 'ticket_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Products', [
            'foreignKey' => 'product_id',
            'joinType' => 'INNER'
        ]);
    }

然后,您只能使用Tickets控制器,并与关联的ProductTickets(工单行项目)一起保存。

因此,您可以在src\Template\Tickets\add.php中设置如下形式:

    <?= $this->Form->create($ticket) ?>
    <fieldset>
        <legend><?= __('Add Ticket') ?></legend>
        <?php
            echo $this->Form->control('client');
            echo $this->Form->control('reference');
        ?>
    </fieldset>
    <fieldset>
        <legend><?= __('Add Product_Tickets (Ticket items)') ?></legend>
       <table>
            <thead>
                <tr>
                    <th><?= __('Product') ?></th>
                    <th><?= __('Qty') ?></th>
                    <th><?= __('Serial Number') ?></th>
                    <th><?= __('Notes') ?></th>
                </tr>
            </thead>

            <tbody>
                <tr>
                <td>
                <?php echo $this->Form->control("product_tickets.0.product_id", []) ?>
                </td>   
                <td>
                <?php echo $this->Form->control("product_tickets.0.qty", []) ?>
                </td>
                <td>
                <?php echo $this->Form->control("product_tickets.0.serial_number", []) ?>
                </td>
                <td>
                <?php echo $this->Form->control("product_tickets.0.notes", []) ?>
                </td>
                </tr>
                <tr>
                <td>
                <?php echo $this->Form->control("product_tickets.1.product_id", []) ?>
                </td>   
                <td>
                <?php echo $this->Form->control("product_tickets.1.qty", []) ?>
                </td>
                <td>
                <?php echo $this->Form->control("product_tickets.1.serial_number", []) ?>
                </td>
                <td>
                <?php echo $this->Form->control("product_tickets.1.notes", []) ?>
                </td>
                </tr>
                <tr>
                <td>
                <?php echo $this->Form->control("product_tickets.2.product_id", []) ?>
                </td>   
                <td>
                <?php echo $this->Form->control("product_tickets.2.qty", []) ?>
                </td>
                <td>
                <?php echo $this->Form->control("product_tickets.2.serial_number", []) ?>
                </td>
                <td>
                <?php echo $this->Form->control("product_tickets.2.notes", []) ?>
                </td>
                </tr>
                <tr>
                <td>
                <?php echo $this->Form->control("product_tickets.3.product_id", []) ?>
                </td>   
                <td>
                <?php echo $this->Form->control("product_tickets.3.qty", []) ?>
                </td>
                <td>
                <?php echo $this->Form->control("product_tickets.3.serial_number", []) ?>
                </td>
                <td>
                <?php echo $this->Form->control("product_tickets.3.notes", []) ?>
                </td>
                </tr>


            </tbody>
            <tfoot>

            </tfoot>
        </table>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>

*(注意,还有其他/更好的方法来创建具有多个关联记录的表单。)

在Tickets控制器中,在add方法中:

    public function add()
    {
        $ticket = $this->Tickets->newEntity();     
        if ($this->request->is('post')) {
            $data = $this->request->getData();
            $ticket = $this->Tickets->newEntity($data, [
                'associated' => ['ProductTickets']
            ]); 
            $ticket = $this->Tickets->patchEntity($ticket, $this->request->getData(),['associated' => 'ProductTickets'
              ]);
            if ($this->Tickets->save($ticket)) {
                $this->Flash->success(__('The ticket has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The ticket could not be saved. Please, try again.'));
        }
        $products = $this->Tickets->ProductTickets->Products->find('list');
        $this->set(compact('ticket', 'products'));
    }

这对我有用,应该保存与ticket_id相关的product_id的任何组合。例如,此请求数据将按预期保存:

debug($this->request->getData());
[
    'client' => '1',
    'reference' => 'reference 1',
    'product_tickets' => [
        (int) 0 => [
            'product_id' => '1',
            'qty' => '12',
            'serial_number' => 'abc1234',
            'notes' => 'note1'
        ],
        (int) 1 => [
            'product_id' => '2',
            'qty' => '5',
            'serial_number' => '4321cba',
            'notes' => 'note2'
        ],
        (int) 2 => [
            'product_id' => '1',
            'qty' => '6',
            'serial_number' => 'a4b3c21',
            'notes' => 'note3'
        ],
        (int) 3 => [
            'product_id' => '3',
            'qty' => '21',
            'serial_number' => '4c3b2a1',
            'notes' => 'note4'
        ]
    ]
]

根据手册,Saving Has Many Associations

重要说明,如果您计划保存在故障单上的4种产品中的任何一种为NULL,则需要从请求数据中删除该产品。您可以使用ProductTicketsTable模型中的beforeMarshal方法来完成此操作。

希望这是您的初衷,对您有所帮助。

干杯

D。