Monday, October 31, 2011

Re: CakePHP 2.0 Model relation alias, am I getting it wrong?

Are you getting an empty array key or no array key? What does your find query look like? Are you using the Containable behaviour? What is recursive set to?

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 31 Oct 2011, at 12:52, John wrote:

Hey guys, I'm rather new in CakePHP although I've developed a couple
small projects back in the 1.1 era.

I've been puzzled with the model relation alias behavior in 2.0
(behavior here used as literal english word not as the cakePHP
entity).

According to the docs one can name different model relation aliases
for the same model class, i.e.

class testExample extends AppModel {
 public $hasMany = array(
'testRelation1' => array( //that's the alias
'className' => 'testRelation', //an existing model here
'foreignKey' => 'test_relation1_id' //existing key in database
),
'testRelation2' => array(
'className' => 'testRelation',
'foreignKey' => 'test_relation2_id' //another existing key in the
database
),
}

When the queries run, they return the expected results but those
results are not shown on page data, probably suppressed at some point.
The "Related Test Relation1" and "Related Test Relation2" come empty.

They only reason to get the data is to have the alias exactly as the
model class name:
 public $hasMany = array(
'testRelation' => array( //same as model class name
'className' => 'testRelation',
'foreignKey' => 'test_relation1_id',
)

Even if you have just one relation like the last example and the alias
is not exactly the model name it doesn't work again.

Is this expected behavior?

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: