3.0: Can't edit associated data #5292

Closed
midorikocak opened this Issue Nov 30, 2014 · 12 comments

Comments

Projects
None yet
4 participants
@midorikocak

I have this controller and edit action:

src/Controller/RecordsController

<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Edit method
 *
 * @param string $id
 * @return void
 * @throws \Cake\Network\Exception\NotFoundException
 */
    public function edit($id = null) {
        $record = $this->Records->get($id, [
            'contain' => ['Addresses','Businesses.Businesstypes','Organizations','Persons','Telephones.Telephonetypes']
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $record = $this->Records->patchEntity($record, $this->request->data,
                ['atomic'=>false,'associated'=>  ['Addresses','Businesses.Businesstypes','Organizations','Persons','Telephones.Telephonetypes']]
                );
            if ($this->Records->save($record, ['atomic'=>false, 'associated'=>  ['Addresses','Businesses.Businesstypes','Organizations','Persons','Telephones.Telephonetypes']])) {
                $this->Flash->success('The record has been saved.');
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error('The record could not be saved. Please, try again.');
            }
        }
        $telephonetypes = $this->Records->Telephones->Telephonetypes->find('list');
        $businesstypes = $this->Records->Businesses->Businesstypes->find('list');
        $this->set(compact('record', 'users','telephonetypes', 'businesstypes'));
    }

The edit.ctp for the record is:

<div class="records form large-10 medium-9 columns">
<?= $this->Form->create($record) ?>
    <fieldset>
        <legend><?= __('Edit Record') ?></legend>
    <?php
        echo $this->Form->input('name');
        echo $this->Form->input('url');
        echo $this->Form->input('description');
        echo $this->Form->input('businesses.0.businesstype_id', ['options' => $businesstypes]);
        echo $this->Form->input('telephones.0.telephonetype_id', ['options' => $telephonetypes]);
        echo $this->Form->input('telephones.0.telephone');
        echo $this->Form->input('addresses.0.streetAddress');
        echo $this->Form->input('addresses.0.addressLocality');
        echo $this->Form->input('addresses.0.postalCode');
        echo $this->Form->input('addresses.0.addressCountry');
        echo $this->Form->input('addresses.0.description',['label'=>__('Address Description')]);
    ?>
    </fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

When I try to submit form I get this data with debug($record)

https://gist.github.com/mtkocak/8706aaeb9fb505f3db9f

var_dump($this->request->data):

array (size=6)
  'name' => string 'Midori Kocak' (length=12)
  'url' => string 'http://www.mtkocak.com' (length=22)
  'description' => string 'My name is Midori!' (length=18)
  'businesses' => 
    array (size=1)
      0 => 
        array (size=1)
          'businesstype_id' => string '1' (length=1)
  'telephones' => 
    array (size=1)
      0 => 
        array (size=2)
          'telephonetype_id' => string '1' (length=1)
          'telephone' => string '+420775259871' (length=13)
  'addresses' => 
    array (size=1)
      0 => 
        array (size=5)
          'streetAddress' => string 'Parizska 5' (length=10)
          'addressLocality' => string 'Prague' (length=6)
          'postalCode' => string '110 00' (length=6)
          'addressCountry' => string 'Czech Republic' (length=14)
          'description' => string 'Home Office' (length=11)

If I do validate->false in save() method, message says it is updated, if not, I get cannot saved error.

@midorikocak midorikocak changed the title from Can't edit associated data to 3.0: Can't edit associated data Nov 30, 2014

@midorikocak

This comment has been minimized.

Show comment
Hide comment
@midorikocak

midorikocak Nov 30, 2014

It saves some of related entities as a new. In example when I do validate=false, it adds a new related entity, but not update the existing one.

It saves some of related entities as a new. In example when I do validate=false, it adds a new related entity, but not update the existing one.

@lorenzo

This comment has been minimized.

Show comment
Hide comment
@lorenzo

lorenzo Nov 30, 2014

Member

Isn't this the same problem as #5292 ?

In that case you just need to provide the data in the right format.

Member

lorenzo commented Nov 30, 2014

Isn't this the same problem as #5292 ?

In that case you just need to provide the data in the right format.

@lorenzo lorenzo added this to the 3.0.0 milestone Nov 30, 2014

@midorikocak

This comment has been minimized.

Show comment
Hide comment
@midorikocak

midorikocak Nov 30, 2014

I solved it. I think we should add some more explanation to documentation. When I added hidden inputs in the view (edit.ctp) it started to edit associated data.

<div class="records form large-10 medium-9 columns">
<?= $this->Form->create($record) ?>
    <fieldset>
        <legend><?= __('Edit Record') ?></legend>
    <?php
        echo $this->Form->input('name');
        echo $this->Form->input('url');
        echo $this->Form->input('description');
        echo $this->Form->hidden('businesses.0.id');
        echo $this->Form->input('businesses.0.businesstype_id', ['options' => $businesstypes]);
        echo $this->Form->hidden('emails.0.id');
        echo $this->Form->input('emails.0.email');
        echo $this->Form->hidden('telephones.0.id');
        echo $this->Form->input('telephones.0.telephonetype_id', ['options' => $telephonetypes]);
        echo $this->Form->input('telephones.0.telephone');
        echo $this->Form->hidden('addresses.0.id');
        echo $this->Form->input('addresses.0.streetAddress');
        echo $this->Form->input('addresses.0.addressLocality');
        echo $this->Form->input('addresses.0.postalCode');
        echo $this->Form->input('addresses.0.addressCountry');
        echo $this->Form->input('addresses.0.description',['label'=>__('Address Description')]);
    ?>
    </fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>

I solved it. I think we should add some more explanation to documentation. When I added hidden inputs in the view (edit.ctp) it started to edit associated data.

<div class="records form large-10 medium-9 columns">
<?= $this->Form->create($record) ?>
    <fieldset>
        <legend><?= __('Edit Record') ?></legend>
    <?php
        echo $this->Form->input('name');
        echo $this->Form->input('url');
        echo $this->Form->input('description');
        echo $this->Form->hidden('businesses.0.id');
        echo $this->Form->input('businesses.0.businesstype_id', ['options' => $businesstypes]);
        echo $this->Form->hidden('emails.0.id');
        echo $this->Form->input('emails.0.email');
        echo $this->Form->hidden('telephones.0.id');
        echo $this->Form->input('telephones.0.telephonetype_id', ['options' => $telephonetypes]);
        echo $this->Form->input('telephones.0.telephone');
        echo $this->Form->hidden('addresses.0.id');
        echo $this->Form->input('addresses.0.streetAddress');
        echo $this->Form->input('addresses.0.addressLocality');
        echo $this->Form->input('addresses.0.postalCode');
        echo $this->Form->input('addresses.0.addressCountry');
        echo $this->Form->input('addresses.0.description',['label'=>__('Address Description')]);
    ?>
    </fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
@midorikocak

This comment has been minimized.

Show comment
Hide comment
@midorikocak

midorikocak Nov 30, 2014

I can close it if you want. But I think associated data issue needs some more detail in controllers and views in documentation.

I can close it if you want. But I think associated data issue needs some more detail in controllers and views in documentation.

@lorenzo

This comment has been minimized.

Show comment
Hide comment
@lorenzo

lorenzo Nov 30, 2014

Member

I agree with you. There is a hole in the documentation around that topic. Would you be interested in helping with that at some point?

Member

lorenzo commented Nov 30, 2014

I agree with you. There is a hole in the documentation around that topic. Would you be interested in helping with that at some point?

@midorikocak

This comment has been minimized.

Show comment
Hide comment
@midorikocak

midorikocak Nov 30, 2014

I think it would be a chapter like a tutorial (but not a tutorial) explaining whole saving editing and deleting associated data, starting from innodb and myisam difference and table relations, how controllers models and views should be. I would like to write that...

I think it would be a chapter like a tutorial (but not a tutorial) explaining whole saving editing and deleting associated data, starting from innodb and myisam difference and table relations, how controllers models and views should be. I would like to write that...

@markstory

This comment has been minimized.

Show comment
Hide comment
@markstory

markstory Dec 1, 2014

Member

I'd rather not go into the differences between MyISAM and InnoDB as that is pretty specific to MySQL, however the rest sounds like a great guide chapter.

Member

markstory commented Dec 1, 2014

I'd rather not go into the differences between MyISAM and InnoDB as that is pretty specific to MySQL, however the rest sounds like a great guide chapter.

@markstory

This comment has been minimized.

Show comment
Hide comment
@markstory

markstory Dec 1, 2014

Member

This feels very related to cakephp/docs#1960.

Member

markstory commented Dec 1, 2014

This feels very related to cakephp/docs#1960.

@htstudios

This comment has been minimized.

Show comment
Hide comment
@htstudios

htstudios Dec 1, 2014

Contributor

👍 for the guide :)

Contributor

htstudios commented Dec 1, 2014

👍 for the guide :)

@lorenzo

This comment has been minimized.

Show comment
Hide comment
@lorenzo

lorenzo Dec 1, 2014

Member

@mtkocak Thanks! Let me know if you need help writing that guide :D

Member

lorenzo commented Dec 1, 2014

@mtkocak Thanks! Let me know if you need help writing that guide :D

@midorikocak

This comment has been minimized.

Show comment
Hide comment
@midorikocak

midorikocak Dec 1, 2014

I forked documentation brach 3 and I will send a pull request after I finish. True?

I forked documentation brach 3 and I will send a pull request after I finish. True?

@lorenzo

This comment has been minimized.

Show comment
Hide comment
@lorenzo

lorenzo Dec 1, 2014

Member

@mtkocak Yes, please do :)

Member

lorenzo commented Dec 1, 2014

@mtkocak Yes, please do :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment