Baked Edit not working


#1

Hi

I have been using cakephp for a long time, but use it at a fairly basic level.

I have a table - called UserBaskets (which has associations with Users and UserBasketItems).

I have baked the CRUD controller and templates.

Here is the Edit method from the UserBasketController

    public function edit($id = null)
    {
        $userBasket = $this->UserBaskets->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $userBasket = $this->UserBaskets->patchEntity($userBasket, $this->request->getData());
            if ($this->UserBaskets->save($userBasket)) {
                $this->Flash->success(__('The user basket has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user basket could not be saved. Please, try again.'));
        }
        $users = $this->UserBaskets->Users->find('list', ['limit' => 200]);
        $basketStatuses = $this->UserBaskets->BasketStatuses->find('list', ['limit' => 200]);
        $this->set(compact('userBasket', 'users', 'basketStatuses'));
    }

And here is the Edit template for that controller:

<?php
/**
 * @var \App\View\AppView $this
 * @var \App\Model\Entity\UserBasket $userBasket
 */
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
    <ul class="side-nav">
        <li class="heading"><?= __('Actions') ?></li>
        <li><?= $this->Form->postLink(
                __('Delete'),
                ['action' => 'delete', $userBasket->ID],
                ['confirm' => __('Are you sure you want to delete # {0}?', $userBasket->ID)]
            )
        ?></li>
        <li><?= $this->Html->link(__('List User Baskets'), ['action' => 'index']) ?></li>
        <li><?= $this->Html->link(__('List Users'), ['controller' => 'Users', 'action' => 'index']) ?></li>
        <li><?= $this->Html->link(__('New User'), ['controller' => 'Users', 'action' => 'add']) ?></li>
        <li><?= $this->Html->link(__('List Basket Statuses'), ['controller' => 'BasketStatuses', 'action' => 'index']) ?></li>
        <li><?= $this->Html->link(__('New Basket Status'), ['controller' => 'BasketStatuses', 'action' => 'add']) ?></li>
        <li><?= $this->Html->link(__('List User Basket Items'), ['controller' => 'UserBasketItems', 'action' => 'index']) ?></li>
        <li><?= $this->Html->link(__('New User Basket Item'), ['controller' => 'UserBasketItems', 'action' => 'add']) ?></li>
    </ul>
</nav>
<div class="userBaskets form large-9 medium-8 columns content">
    <?= $this->Form->create($userBasket) ?>
    <fieldset>
        <legend><?= __('Edit User Basket') ?></legend>
        <?php
            echo $this->Form->control('user_id', ['options' => $users]);
            echo $this->Form->control('basket_name');
            echo $this->Form->control('basket_status_id', ['options' => $basketStatuses, 'empty' => true]);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

This is all straight out of bake.
When I try to update the basket_status_id using the above template, no error is reported, and the success Flash message is shown.

BUT, and this is the infuriating bit - the actual basket’s basket_status_id is NOT updated, it remains the same.

No errors, nothing. And I am not sure how to track this problem.

Any suggestions or similar experiences would be welcome!

Thanks


#2

check _accessible in UserBasket entity in App\Model\Entity


#3

Brilliant, many thanks Graziel - I had added the basket_status_id field after having baked the models. New to version 3, so missed that out. It is working beautifully now!