Tuesday, February 24, 2015

Re: Still having trouble with saving BelongsToMany data

Okay, i've tried again, but i'm still not there. i think i need a little more detail / hand-holding...

LISTING
Entity:
protected $_accessible = [
   
// All table columns = true, except id (not listed)
   
// ...
   
'attributes' => true
];

Table:
public function initialize() {
   
// Calls to table, primaryKey, displayField, belongsTo for other tables...
    $this
->belongsToMany('Attributes', [
       
'foreignKey' => 'listing_id',
       
'targetForeignKey' => 'attribute_id',
       
'joinTable' => 'listings_attributes'
   
]);
   
// i thought all that could be handled automatically with
   
// $this->belongsToMany('Attributes', ['through' => 'ListingsAttributes']); ?
}

ATTRIBUTES
Entity:
protected $_accessible = [
   
// All table columns = true, except id (not listed)
   
'listings' => true
];

Table:
public function initialize() {
   
// Calls to table, displayField, primaryKey
    $this
->belongsToMany('Listings', [
       
'foreignKey' => 'attribute_id',
       
'targetForeignKey' => 'listing_id',
       
'joinTable' => 'listings_attributes'
   
]);
   
// Again, i thought this could be done with
   
// $this->belongsToMany('Listings', ['through' => 'ListingsAttributes']);
}

LISTINGS_ATTRIBUTES
Entity:
protected $_accessible = [
   
'listing_id'   => true,
   
'attribute_id' => true,
   
'value'        => true,
   
'listing'      => true,
   
'attribute'    => true,
];

Table:
public function initialize() {
   
// Calls to table, displayField, primaryKey
    $this
->belongsTo('Listings', ['foreignKey' => 'listing_id']);
    $this
->belongsTo('Attributes', ['foreignKey' => 'attribute_id']);
}

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

public function buildRules(RulesChecker $rules) {
    $rules
->add($rules->existsIn(['listing_id'], 'Listings');
    $rules
->add($rules->existsIn(['attribute_id'], 'Attributes');
   
return $rules;
}

Finally, ListingsController:
// Simplified add():
public function add() {
    $listing
= $this->Listings->newEntity();
   
if ($this->request->is('post')) {
        $listing
= $this->Listings->patchEntity($listing, $this->request->data);

       
if ($this->Listings->save($listing)) {
           
// Flash->success & return redirect to index
       
} else {
           
// Flash->error
       
}
   
}

   
// various association data lookups for form data
   
// stuff like $attributes = $this->Listings->findListByActive(true)->order('name');
   
// using magic finders

    $this
->set(compact(/* all the variables from the finders */));
}

Note that nowhere in the baked code is the ['associated' => ['Attributes._joinData']] option used as it is in the documentation. i HAVE tried it in either newEntity or patchEntity calls, and both together. No luck.

~whew~

The result of all this: the Listings record saves. But i'd already gotten that far before. Still no ListingsAttributes records.
  1. Your last post mentions removing validation for the two foreign key columns in ListingsAttribute... So that means removing the add/requirePresence/notEmpty calls for them?
  2. i left my HTML/Form alone (see original post for what that looks like), because i think there's a bug specifying a connection for cake bake template... If the current documentation is accurate, that Form build should work, correct?
  3. i'm not sure what you meant by allowing id for attributes... i'm not trying to create new attributes in this form, just write the joint table records... Please clarify?
i hope i'm almost there... i will be SO happy when i can finally move forward with this project again...
Thanks.
-joe


On Tuesday, 24 February 2015 02:09:55 UTC-5, heavyKevy wrote:
Joe,
Looking more carefully, I see that the tickets are recently closed.

I did a composer update --prefer-dist,

rebaked the listingsAttributes controller and templates,
removed the validation for attribute_id and listing_id from the listings_Attributes table and it is saving the listings and the listing attributes correctly.

Note: don't forget to allow the id for the attributes or it will recreate the attributes every time.

--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: