People are so lazy these days that they cant even search the group
before asking the question for the 10th time...

On 4/30/08, daphonz <[EMAIL PROTECTED]> wrote:
>
> Yes.  You can use the "with" setting in your HABTM many model setup
> for this.  For example, in your foo model, you can sort your HABTM
> entries by a unique ordering stored in the join table:
>
> class FooModel extends AppModel {
>       var $hasAndBelongsToMany = array('Bar' =>
>                                array('className'    => 'Bar',
>                                            'with'=>'BarredFoo',
>                                      'joinTable'    => 'bar_foo',
>                                      'foreignKey'   => 'foo_id',
>                                      'associationForeignKey'=>
> 'bar_id',
>                                      'conditions'   => '',
>                                      'order'        =>
> array('BarredFoo.ordering'=>'ASC'),
>                                      'limit'        => '',
>                                      'unique'       => true,
>                                      'finderQuery'  => '',
>                                      'deleteQuery'  => '',
>                                )
> }
>
> Here the model, "BarredFoo," is a model defined as such:
>
> <?php
> class BarredFoo extends AppModel
> {
>     var $name = 'BarredFoo';
>     var $useTable = 'bar_foo';
>
> }
> ?>
>
> To save extra data to the jointable, you can use a custom SQL query.
> You can make functions in your new join Model:
> <?php
> class BarredFoo extends AppModel
> {
>     var $name = 'BarredFoo';
>     var $useTable = 'bar_foo';
>
>      function updateOrdering($associated_id,$content_id,$ordering) {
>                       $content_key = 'foo_id';
>                       $associated_key = 'bar_id';
>                       return $this->query("UPDATE `".$this->useTable."`
> SET `".$associated_key."`='".$associated_id."', `".$content_key."`='".
> $content_id."', `ordering`='".$ordering."' WHERE `".$associated_key."`
> = '".$associated_id."' AND `".$content_key."` = '".$content_id."';");
>               }
>
> }
> ?>
>
> And you can call this function in the afterSave callback in your
> initial model:
>
> <?php
> class FooModel extends AppModel {
>       var $hasAndBelongsToMany = array('Bar' =>
>                                array('className'    => 'Bar',
>                                            'with'=>'BarredFoo',
>                                      'joinTable'    => 'bar_foo',
>                                      'foreignKey'   => 'foo_id',
>                                      'associationForeignKey'=>
> 'bar_id',
>                                      'conditions'   => '',
>                                      'order'        =>
> array('BarredFoo.ordering'=>'ASC'),
>                                      'limit'        => '',
>                                      'unique'       => true,
>                                      'finderQuery'  => '',
>                                      'deleteQuery'  => '',
>                                )
>
>      function afterSave()
>      {
>              if (isset($this->data['Bar']['Bar'][0])) {
>                    foreach ($this->data['Bar']['Bar'] as $key =>
> $bar_id) {
>                         $this->BarredFoo->updateOrdering($bar_id,$this-
> >id,$key);
>                    }
>             }
>             return true;
>       }
> }
> ?>
>
> That's how I order my HABTM relationships, but obviously you could
> extend this ad absurdum.
>
> A good article to to read about basic With relationships is here:
> http://www.littlehart.net/atthekeyboard/2007/09/04/a-glimpse-inside-cakephp-12/
>
> Hope this helps,
>
> Casey
>
> On Apr 30, 9:24 am, grigri <[EMAIL PROTECTED]> wrote:
> > For retrieval, this is easy: just add the extra columns to the join
> > table; you can optionally create a model file for the join if you need
> > to. Do a debug() on the fetched data array and you'll see all the
> > extra fields in there.
> >
> > It's putting the extra data in there easily that's more complicated -
> > the normal form helper / model __saveMulti() combo don't play nicely
> > with extra fields, afaik.
> >
> > On Apr 30, 1:23 pm, tgies <[EMAIL PROTECTED]> wrote:
> >
> > > foo HABTM bar, but I also need to store some specific information
> > > about that association -- say, a user HABTM groups, but each user also
> > > has a role within that group and a date of joining that group which I
> > > need to store in the foos_bars table. How do I get CakePHP to clue
> > > into this and pull out that information from the foos_bars table?
> >
>


-- 
Marcin Domanski
http://kabturek.info

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