I really like that approach so here's essentially what I did:

photos
 - id
 - owner_model (varchar(20))
 - owner_id

<?php
class Photo extends AppModel
{
    var $name = 'Photo';
    var $belongsTo = array(
        'Person' => array(
            'foreignKey' => 'owner_id',
            'conditions' => array('Photo.owner_model' => 'Person')),
        'Group' => array(
            'foreignKey' => 'owner_id',
            'conditions' => array('Photo.owner_model' => 'Group'))
    );
}
?>

<?php
class Person extends AppModel
{
    var $name = 'Person';
    var $hasMany = array(
        'Photo' => array(
            'foreignKey' => 'owner_id',
            'conditions' => array('Photo.owner_model' => 'Person')
        );
    );
}
?>


The belongsTo on Photo works great.  The hasMany on Person works in
the simple test I tried with just those 3 models, but in my larger
more complex app it didn't work - still trying to find out why.  It
keeps sticking the Photo.owner_model='Person' condition on every SQL
query, even ones not involving Photo...


On 5/4/07, Mariano Iglesias <[EMAIL PROTECTED]> wrote:
>
> You could also add a model and id field, and set the belongsTo conditions in
> Photo specifying foreignKey and conditions.
>
> So you add two fields to table photos:
>
> model
> record_id
>
> And then on Photo model:
>
> <?php
>
> class Photo extends AppModel
> {
>         var $name = 'Photo';
>         var $belongsTo = array(
>                 'Person' => array(
>                         'foreignKey' => 'record_id',
>                         'conditions' => 'Photo.model = \'Person\''
>                 ),
>                 'Group' => array(
>                         'foreignKey' => 'record_id',
>                         'conditions' => 'Photo.model = \'Group\''
>                 )
>         );
> }
>
> ?>
>
> http://manual.cakephp.org/chapter/models
>
> -MI
>
> ---------------------------------------------------------------------------
>
> Remember, smart coders answer ten questions for every question they ask.
> So be smart, be cool, and share your knowledge.
>
> BAKE ON!
>
> blog: http://www.MarianoIglesias.com.ar
>
>
> -----Mensaje original-----
> De: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] En nombre
> de Zach Cox
> Enviado el: Viernes, 04 de Mayo de 2007 03:32 p.m.
> Para: cake-php@googlegroups.com
> Asunto: Re: Polymorphic Associations
>
> Imagine a social networking site with people, groups, & photos:
>  - Person hasMany Photo
>  - Group hasMany Photo
>  - Photo belongsTo Person, Group
>
> Then you'd need to do:
>
> photos
>  - id
>  - person_id
>  - group_id
>
>
> >
>

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

Reply via email to