Re: Need help with less trivial associations
Thanks for the response. The groups_units association is needed because there are units that don't belong to any department but only to groups. I assume the example you gave is for cake 1.2 ? I am using 1.1 and can't find that Set::extract in the API. I will try and figure out how to do this in 1.1, unless someone can offer help with the 1.1 equivalent. Also, what would be the best place to put this code assuming I want every Unit to have the full list of unique groups once the Unit model is read from the database? Thanks. On Mar 4, 2:06 am, Grant Cox [EMAIL PROTECTED] wrote: Well, I would scrap the groups_units HABTM association, as it isn't really a direct association. You can't add/remove those associations directly, so it's really just a symptom of the main Group - Department - Units association. Now, to get the associated group ids from a unit, have something like: $this-Unit-recursive = 2; // get the Departments, and the Departments' Groups $unit = $this-Unit-read(null, $unit_id); $group_ids = Set::extract($unit, 'Department.{n}.Group.{n}.id'; $unique_group_ids = array_keys(array_flip($group_ids)); --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Need help with less trivial associations
Well, if you really need the groups_units association then you will have two associations to manage, and it's probably going to be a pain to ensure that these are up to date. You can have callbacks for afterSave and afterDelete (or is there only beforeDelete?) on the various models to insert/remove some groups_units, but I don't think that will be comprehensive. But once you've got that working, getting the count should be somewhat simpler - as it's just querying across one join. You can either do this with a manual query, or just do a read() and then count the associated rows. If you want fields to be added to every model that is loaded, you need to do this in the afterFind callback. Have a look at the second option I posted in http://groups.google.com/group/cake-php/browse_thread/thread/f4efae3f1bd46930 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Need help with less trivial associations
Well, I would scrap the groups_units HABTM association, as it isn't really a direct association. You can't add/remove those associations directly, so it's really just a symptom of the main Group - Department - Units association. Now, to get the associated group ids from a unit, have something like: $this-Unit-recursive = 2; // get the Departments, and the Departments' Groups $unit = $this-Unit-read(null, $unit_id); $group_ids = Set::extract($unit, 'Department.{n}.Group.{n}.id'; $unique_group_ids = array_keys(array_flip($group_ids)); --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Cake PHP group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---