Wednesday, February 25, 2015

Re: Still having trouble with saving BelongsToMany data

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: