Tuesday, September 2, 2014

Saving unique data in hasMany through model

Here's the associations that I have set up:

Partner hasMany PartnerInvolvement
Product hasMany PartnerInvolvement
PartnerInvolvementType hasMany PartnerInvolvement
PartnerInvolvement belongsTo Partner, Product, PartnerInvolvementType

The $hasMany relationship is defined the same way in all 3 of the Partner, Product, PartnerInvolvementType models:
class Product {
public $hasMany = array(
'PartnerInvolvement' => array(
'className' => 'PartnerInvolvement',
'foreignKey' => 'product_id',
'dependent' => true,
'unique' => 'keepExisting'
)
); 
}


When adding/editing a new Product, I want to be able to choose which Partners are involved with that Product, which PartnerInvolvementType it is, and create new PartnerInvolvement relationships from that data.

Here is the data that I am trying to save:
Array  (      [Product] => Array          (              [id] => 2981              [name] => Laurie Amerson Rental itr1229              [product_type_id] => 12              [default_price] => 0.00          )        [PartnerInvolvement] => Array          (              [0] => Array                  (                      [partner_id] => 1                      [partner_involvement_type_id] => 1                  )                [1] => Array                  (                      [partner_id] => 2                      [partner_involvement_type_id] => 1                  )            )    )    My app is creating a new PartnerInvolvement entry any time I try to save this data.  I am trying to restrain it so that a unique combination of product_id, partner_id, partner_involvement_type_id is only saved once.  Any suggestions?  Should I go back to using a HABTM instead of a hasMany through relationship?

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