Saturday, January 1, 2011

Dynamically bind HABTM association - Cakebook example not working

Hi,

I am trying to dynamically bind HABTM association using hasOne.

I followed the Recipes example with hasOne at following cakebook URL
http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM

Here is my code:
$this->Post->bindModel(array(
'hasOne' => array(
'PostsTag',
'FilterTag' => array(
'className' => 'Tag',
'foreignKey' => false,
'conditions' => array('FilterTag.id = PostsTag.tag_id')
))));
$output=$this->Post->find('all', array(
'fields' => array('Post.*')
));
debug($output);

Its not working as shown in cakebook example.
I am getting output like below without Tags at all.
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)

)

[1] => Array
(
[Post] => Array
(
[id] => 2
[title] => test post2
)

)
)


I was expecting output like below:
Array
(
0 => Array
{
[Post] => Array
(
[id] => 1
[title] => test post 1
)
[Tag] => Array
(
[0] => Array
(
[id] => 1
[name] => php
)
[1] => Array
(
[id] => 2
[name] => javascript
)
[2] => Array
(
[id] => 3
[name] => xml
)
)
}

My aim is to bind 'Tag' model dynamically in controller action.
I know it will work if I declare var $hasAndBelongsToMany =
array('Tag') beforehand.
Any help would be appreciated.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

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
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: