How to get HABTM associated data using hasOne binding
I am following example documented at http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM
I am trying to retrieve associated data using hasOne.
I created 3 tables posts, tags and posts_tags. I wrote following code to debug Posts.
$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);
I was expecting output something 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
)
)
}
But my output do not have Tags at all. Here is what I got.
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
)
[1] => Array
(
[Post] => Array
(
[id] => 2
[title] => test post2
)
)
)
How do I get associated tags along with the post.
I know I am missing something, but unable to figure out. Any help would be highly appreciated.
Edit 1:
Ok 开发者_开发百科I tried few more variants.
I tried:
$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');
I got:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[PostsTag] => Array
(
[id] => 1
[post_id] => 1
[tag_id] => 1
)
[FilterTag] => Array
(
[id] => 1
[name] => php
)
)
[1] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[PostsTag] => Array
(
[id] => 2
[post_id] => 1
[tag_id] => 2
)
[FilterTag] => Array
(
[id] => 2
[name] => javascript
)
)
[2] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[PostsTag] => Array
(
[id] => 3
[post_id] => 1
[tag_id] => 3
)
[FilterTag] => Array
(
[id] => 3
[name] => xml
)
)
)
I tried:
$output=$this->Post->find('all', array(
'fields' => array('Post.*', 'FilterTag.*'),
'recursive' => 1
));
I got:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[FilterTag] => Array
(
[id] => 1
[name] => php
)
)
[1] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[FilterTag] => Array
(
[id] => 2
[name] => javascript
)
)
[2] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[FilterTag] => Array
(
[id] => 3
[name] => xml
)
)
)
Just in case I am missing something, here is my Posts controller:
class PostsController extends AppController {
var $name = 'Posts';
var $helpers = array('Html','Ajax','Javascript');
var $components = array( 'RequestHandler' );
function index() {
$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.*', 'FilterTag.*'),
'recursive' => 1
));
}
}
And here is my Posts model:
class Post extends AppModel {
var $name = 'Post';
}
I still wonder why is the example from cookbook not working.
I would recommend checking 3 things:
See if you have set your associations right (
hasMany
,hasAndBelongsToMany
)Make sure you have set the Post HABTM Tag and Tag HABTM Post relationships correctly.
Check the value of
$recursive
Most of the examples in the CakePHP book require that you have your
$recursive
property set to 1.Remove the fields restriction:
'fields' => array('Post.*')
It's possible that Cake is restricting only the
Post
fields, which is why noTag
fields are showing up. Remove this restriction and try it again.
Let us know if this works for you.
精彩评论