1

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

我试图在模型中配置“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'
            ]
        ]
    ]
]

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

我该如何解决这个问题?

4

1 回答 1

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'
        ]);
    }

然后您可以仅使用门票控制器并与关联的 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() ?>

*(请注意,还有其他/更好的方法可以制作具有多个关联记录的表单。)

在门票控制器中,在 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_ids 的任何组合。例如,此请求数据按预期保存:

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'
        ]
    ]
]

根据手册,保存有许多关联

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

希望这是您的想法并且对您有所帮助。

干杯,

D.

于 2019-04-30T15:37:40.793 回答