Monday, March 2, 2015

Re: Still having trouble with saving BelongsToMany data

i'm REALLY sorry for the delay getting back to you. And unfortunately, i'm still not there. It's CLOSE though:

i did exactly as you suggested in your last post. Set up my Entities & Tables just as you described. Set up the controller to manually set the _joinData "dirty" and called the save.

i get the ListingsAttributes record now! But no value from the text input (_joinData.value).

The POST data is correct:

Array
(
   
[attributes] => Array
       
(
           
[3] => Array
               
(
                   
[id] => 1
                   
[_joinData] => Array
                       
(
                           
[value] => Off-street
                       
)
               
)
       
)
)

That's exactly what i've been getting since i change to use _joinData...

Here's the Entity after merging & setting _joinData dirty:

Array
(
   
[attributes] => Array
       
(
           
[0] => Array
               
(
                   
[id] => 1
               
)
       
)
)

As before, the "value" input got lost. However, there is FINALLY an Attributes array in the Listing entity with the correct Attributes.id. The result is, i get a ListingsAttributes record with the Listings.id and Attributes.id, but nothing in the value column.

This is making me crazy & i'm on the edge of scrapping this thing & starting over with something else. i never expected this much difficulty getting some basic data relationships to work after following documentation exactly (and then having to make undocumented changes that still only get me part-way there. i don't think i'm doing anything radical or unconventional with my form, so i don't understand the difficulty of making the ORM function as advertised.

i'm frustrated, obviously. It's taken me this long to come back to follow up because it becomes harder & harder to come back & try again. i'm almost there, i just don't know why the value input won't go into the Entity data with everything else.

If you're moving on, i totally understand. But if you'd like to look at the code directly, let me know. i'll have to put a version online without sensitive info in it...

Thanks for at least getting me this far. :)
-joe




On Wednesday, 25 February 2015 04:57:35 UTC-5, heavyKevy wrote:
Joe,

Here is what I did:
In the attribute entity:
   protected $_accessible = [
        'id' => true,
        'name' => true,
        'listings' => true,
    ];

In the Listings_Attributes Table:
   public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create')
            ->add('listing_id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('listing_id', 'create')
            ->notEmpty('listing_id')
            ->add('attribute_id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('attribute_id', 'create')
            ->notEmpty('attribute_id')
            ->allowEmpty('value');

        return $validator;
    }


In the Listings Controller:  Add method:
    public function add()
    {
        $listing = $this->Listings->newEntity();
        if ($this->request->is('post')) {
            $listing = $this->Listings->patchEntity($listing, $this->request->data,['associated'=>['attributes._joinData']]);
            $listing->dirty('attributes._joinData',true);
            if ($this->Listings->save($listing)) {
                $this->Flash->success('The listing has been saved.');
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error('The listing could not be saved. Please, try again.');
            }
        }
        $attributes = $this->Listings->Attributes->find('list', ['limit' => 200]);
        $this->set(compact('listing', 'attributes'));
        $this->set('_serialize', ['listing']);
    }

In the Add template of the Listing: add.ctp
 <?= $this->Form->create($listing); ?>
    <fieldset>
        <legend><?= __('Add Listing') ?></legend>
        <?php
            echo $this->Form->input('name');
            //echo $this->Form->input('attributes._ids', ['options' => $attributes]);
           
            if(!empty($attributes)){
                $i =0;
                foreach( $attributes as $k =>  $attr){
                              echo $this->Form->input('attributes.'.$i.'.id',['type'=>'checkbox','value'=>$k,'label'=>$attr]);
                              echo $this->Form->input('attributes.'.$i.'.name',['type'=>'hidden','value'=>$attr]);
                              echo $this->Form->input('attributes.'.$i.'._joinData.value');
                              $i++;
                          }
                        }
            //echo $this->Form->select('attributes._ids', $attributes,['label'=>'Attributes','multiple' => 'checkbox']);
           
           
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>


I don't believe I changed anything else.. Just used what was baked, and it works for me.

--Kevin

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: