Google Groups Home
Help | Sign in
Message from discussion Polymorphic not returning associated fields
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Dardo Sordi Bogado  
View profile
 More options Apr 1, 4:55 pm
From: "Dardo Sordi Bogado" <dardoso...@gmail.com>
Date: Tue, 1 Apr 2008 17:55:44 -0300
Local: Tues, Apr 1 2008 4:55 pm
Subject: Re: Polymorphic not returning associated fields
There is a bug in the behavior that make it fetch only id and
displayfield (that is a virtual field and is documented in the
behavior. ).

In the line 45, change:

                                                array('id', $model->$class->displayField), null, -1);
to:

                                                null, null, -1);

Link to fixed version:

http://bin.cakephp.org/view/1702492171

This is the patch:

----------BEGIN-PATCH-------------------
--- polymorphic_original.php    2008-04-01 17:57:56.000000000 -0300
+++ polymorphic.php     2008-04-01 17:58:50.000000000 -0300
@@ -42,7 +42,7 @@
                                                )));
                                        }
                                        $associated = $model->$class->find(array($class . '.id' => $foreignId),
-                                               array('id', $model->$class->displayField), null, -1);
+                                               null, null, -1);
                                        $associated[$class]['display_field'] =
$associated[$class][$model->$class->displayField];
                                        $results[$key][$class] = $associated[$class];
                                }
-----------END-PATCH-------------------

On Tue, Apr 1, 2008 at 12:38 PM, rob5408 <rob5...@gmail.com> wrote:

>  > 1. A litle background about what are you trying to do.

>  I have Person information that is common to all people (name,
>  nationality, birthdate). But People can be different things though,
>  like Coaches, Players, Officials etc. These Person Types can have
>  statistical information like player stats for players or win/loss
>  records for coaches. I didn't want to lump unnecessary information
>  onto Person that didn't need it , so I broke each of these types out
>  to sep. tables and models

>  > 2. The code you are using to define the models.

>  class Person extends AppModel {

>         var $name = 'Person';
>         var $actsAs = array('Polymorphic');

>         // The database table for this model includes 'class' and
>  'foreign_id' fields
>         // No associations to person types exist here
>  }

>  class CoachRating extends AppModel {

>         var $name = 'CoachRating';

>         var $hasMany = array(
>                 'Person' => array(
>                         'className' => 'Person',
>                         'foreignKey' => 'foreign_id',
>                         'conditions' => array('Person.class' => 'CoachRating'),
>                         'dependent' => true
>                 )
>         );

> }

>  > 3. How are you calling the models.

>  When I go to view one Person in particular, I use

>  $this->set('person', $this->Person->read(null, $id));

>  > 4. The SQL that is beign generated.

>  SELECT `Person`.`id`, `Person`.`birthdate`, `Person`.`firstname`,
>  `Person`.`lastname`, `Person`.`country_id`, `Person`.`person_type_id`,
>  `Person`.`class`, `Person`.`foreign_id` FROM `persons` AS `Person`
>  WHERE `Person`.`id` = 4045 LIMIT 1
>  SELECT `CoachRating`.`id` FROM `coach_ratings` AS `CoachRating` WHERE
>  `CoachRating`.`id` = 8 LIMIT 1

>  > 5. The version of cake you are using.

>  1.2.0.6311 beta

>  Thanks! rob

>  On Apr 1, 11:24 am, "Dardo Sordi Bogado" <dardoso...@gmail.com> wrote:
>  > Missing information:

>  > 1. A litle background about what are you trying to do.
>  > 2. The code you are using to define the models.
>  > 3. How are you calling the models.
>  > 4. The SQL that is beign generated.
>  > 5. The version of cake you are using.

> > On Tue, Apr 1, 2008 at 12:11 PM, rob5408 <rob5...@gmail.com> wrote:

>  > >  I followed the Bakery page on setting up my db and models for the
>  > >  Polymorphic Behavior and it looks like I'm almost there, but the rest
>  > >  of the db fields for my associated model aren't being included. I'm
>  > >  seeing a return like this...

>  > >  Array
>  > >  (
>  > >     [Person] => Array
>  > >         (
>  > >             [id] => 4045
>  > >             [birthdate] => 1978-04-11
>  > >             [firstname] => Donald
>  > >             [lastname] => Marks
>  > >             [class] => CoachRating
>  > >             [foreign_id] => 8
>  > >         )

>  > >     [CoachRating] => Array
>  > >         (
>  > >             [id] => 8
>  > >             [display_field] => 8
>  > >         )

>  > >  )

>  > >  but 'display_field' isn't a field in CoachRating table and there's two
>  > >  fields omitted. Maybe I'm trying to use this behavior when another
>  > >  would be more logical. Thanks, rob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google