If you have set up both the Member and County models with
$hasAndBelongsToMany, you should be able to do away with your joining
model CountiesMembers (using the table "counties_members" I'm
presuming). Since you are going through and saving Counties to the
Members, try something roughly like this:
$counties = array();
foreach ($counties as $county) {
$counties[] = $county['County']['id'];
}
// $this->Member->id must be set
$this->Member->save(array('County' => array('County' =>
$counties)));
Don't ask me why it's like that, but it is. So HABTM in both models,
and a double-nested array of IDs will save the associations. Keep in
mind that this will first destroy all the entries for that particular
member then insert the new ones.
On Jul 8, 5:09 am, Alastair <[email protected]> wrote:
> Hi all,
>
> Firstly, I do understand HABTM relationships, having come from a Rails
> background and having built sites with these kind of relationships.
> What I'm struggling with is the CakePHP way of accessing and creating
> relationships.
>
> I have two models, Member and County, and have created a
> counties_members join table.
>
> My counties table is populated with data and is simply structured like
> id, name. I also have a number of member records in my database. What
> I'm trying to do is associate and establish a Member relationship with
> zero, one or more Counties.
>
> I am looping through an array which contains county IDs that I know
> the Member will have. I've tried a number of different approaches and
> am now trying to fill the counties_members table with IDs and have
> written this code.
>
> $county_insert = array();
>
> foreach($county_records as $county)
> {
> $county_insert['CountiesMember']['county_id'] = $county['County']
> ['id'];
> $county_insert['CountiesMember']['member_id'] = $this->Member->id;
>
> $this->Member->CountiesMember->bindModel(array('belongsTo' => array
> ('County')));
> $this->CountiesMember->create();
> $this->CountiesMember->save($county_insert);
> unset($count_insert);
>
> }
>
> The error I'm getting is:
>
> Undefined property: MembersController::$CountiesMember [APP/
> controllers/members_controller.php, line 175]
>
> Fatal error: Call to a member function create() on a non-object in /
> www/landscaper.org.uk/app/controllers/members_controller.php on line
> 175
>
> If anyone has a solution to my HABTM problem, that would be fantastic!
> If there is a better way of doing this, then by all means please
> enlighten me!
>
> Many thanks in advance,
>
> Alastair
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---