Hi Everyone,

I had a recent question here regarding Finding records via conditions in habtm <http://stackoverflow.com/questions/4268300/finding-records-via-conditions-in-habtm>. Now, I could search for posts within a category I searched for.

Now, my questions is how to I retrieve the categories of each post if it has two or more categories in this query:

|$this->set('posts',  $this->Category->find(
    'first',
    array(
        'conditions'  =>  array(
            'Category.uri'  =>  $uri
        ),
        'contain'  =>  array('Post')
    )
));
|

I'd imagine something like this:

|$this->set('posts',  $this->Category->find(
    'first',
    array(
        'conditions'  =>  array(
            'Category.uri'  =>  $uri
        ),
        'contain'  =>  array('Post'  =>  array(
            'contain'  =>  'Category'
        ))
    )
));
|

Here's what my models look like.

|// Category Model
class  Category  extends  AppModel  {
    var  $name=  'Category';
    var  $hasAndBelongsToMany=  array(
        'Post'  =>  array(
            'className'  =>  'Post'
        )
    );
    var  $actsAs=  array('Containable');
}

// Post Model
    class  Post  extends  AppModel  {
        var  $name=  'Post';
        var  $hasAndBelongsToMany=  array(
            'Category'  =>  array(
                'className'  =>  'Category'
            )
        );
        var  $actsAs=  array('Containable');
        var  $virtualFields=  array(
            'date_posted'  =>  'DATE_SUB(Post.created, INTERVAL 7 DAY)'
        );
    }
|

A sample data would be like this:

|categories
id name
1    holidays
2    destinations

posts
id title
1    I am a post
2    I am another post

categories_posts
post_id category_id
1         1
2         2
2         1
|

I am retrieving posts from the Category, 'holidays'.

|Array
(
    [Category]  =>  Array
        (
            [id]  =>  3
            [name]  =>  holidays
            [uri]  =>  holidays
            [created]  =>  2010-11-25  20:43:03
            [modified]  =>  2010-11-25  20:43:03
        )

    [Post]  =>  Array
        (
            [0]  =>  Array
                (
                    [id]  =>  1
                    [title]  =>  I am a post
                ),
            [1]  =>  Array
                (
                    [id]  =>  2
                    [title]  =>  I am a another post
                )
        )
)
|

The problem is that 1 of the posts are in two categories. I'd like to have that information also available so that I can show this in the view

/categories/view/holidays

I am a post
categories: holidays, destination
I am a body text


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 [email protected]
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