Hey XuMiX,

with cake u are able to save meta data within join table, but it's
somehow a tricky part. I suggest you to read (http://cricava.com/blogs/
index.php?
blog=6&title=modelizing_habtm_join_tables_in_cakephp_&more=1&c=1&tb=1&pb=1)
to get the point.

First of all when saving habtm related data the join-table is auto-
modelized for you. If you aim to save meta data in the join table, you
will surely like to validate it as well, so good start is to create
model for your join table (in your case GroupsPerson, note: convention
is person => people), where you can define belongsTo associations to
habtm related models so as validation for the meta-data and one more
important thing which i will cover later.

Now when you have your join-table model i assume you are familiar with
saving habtm data without meta data in join table (reminder: $this-
>data['RelatedModel'] = array of associated ids). In case of meta-data
thing you just have to format that passed array other way so for your
example if you are saving new Group and it's associated people it
would be like this:

array(
        0 => array(
                person_id => XYZ,
                year => 123541,
                amount = 123123
        ),

        1 => array(
                person_id => ABC,
                year => 1212,
                amount = 123
        )

        ...
);

Last and most important thing is when workin with meta data in join
table. You have to change the database structure of it. Basicaly join-
table is a weak entity type consisting of foreign keys of related
tables which are together creating primary key of the join table.
Unfortunatelly cake don't know how to handle multicolumn primary keys
so you have to create a autoincrementing ID primary key for your join
table, and the associated foreign keys, well they just stay FKs. For
your case it will be

groups_people ( PK(id), FKs(group_id, person_id) )

This way everything will work ok and cakes sqls will be generated
properly.

Remember the one important thing i've noticed up there ? Ya you now
have to ensure that the changed join-table still works as it should,
that means it have correct data. The fact the primary key is now ID it
allows you to save multiple same associations which is not good. You
can avoid this by adding a beforeSave callback to the join-table model
and aborting the save process if you find already existing combination
of FKs in the join table.

Hope that helps.
d.

On 11. Júl, 09:19 h., XuMiX <[EMAIL PROTECTED]> wrote:
> and one more thing.... according to this code in model.php
>                                 if (!empty($newValues)) {
>                                         $fields = join(',', array(
>                                                 
> $db->name($this->hasAndBelongsToMany[$assoc]['foreignKey']),
>                                                 
> $db->name($this->hasAndBelongsToMany[$assoc]
> ['associationForeignKey'])
>                                         ));
>                                         $db->insertMulti($this->{$join}, 
> $fields, $newValues);
>                                 }
> there is no way to save additional data
>
> On 2 июл, 23:23, James K <[EMAIL PROTECTED]> wrote:
>
> > CakePHP has no problem reading extra fields in your join table. As
> > long as you're naming your join table correctly and have a
> > hasAndBelongsToMany relationship to the model on the other side of the
> > join table, it'll pull the extra fields from a find automatically.
>
> > On Jul 2, 5:35 am,XuMiX<[EMAIL PROTECTED]> wrote:
>
> > > i have a habtm relation, persons, groups, and a relation table
> > > groups_persons
> > > i need to add some more info into the relation table, for instance
> > > year of presence, role, etc.
> > > how do i get that info within cake ? Or how do i organize my tables
> > > and models?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to