Hello,

I have a problem with a couple of models that share a HABTM
relationship. I've tried two different approaches, but none provide
the desired result. Here are they both, I hope you can help me to fix
either of them to get the array I need.

I'm using the good old
Post hasAndBelongsToMany Tag

So, let's say I have three posts:
Post #1 has Tags 'cool' and 'cakephp'
Post #2 has Tag 'cakephp'
Post #3 has Tag 'cool' 'dessert'

This is the complicated part. What I want to achieve is to get a
resultset (array) that has ONLY the posts which DO NOT JUST contain
the Tag 'cakephp', and list their tags. i.e., something that looks
like this

Array
(
    [0] => Array
        (
            [Post] => Array
                (
                    [id] => 1
                    [title] => Post1
                )

            [Tag] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => cool

                        )

                )

        )
    [1] => Array
        (
            [Post] => Array
                (
                    [id] => 3
                    [title] => Post3
                )

            [Tag] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => cool

                        )
                    [1] => Array
                        (
                            [id] => 3
                            [name] => dessert

                        )

                )

        )

My first approach was to use the Containable behavior. It helps me
limit the Tags, but it doesnt let me filter the Posts.

$this->Post->find('all',
  array('contain' => array(
                'Tag' => array(
                        'conditions' => array('not'=> array('Tag.name' => 
'cakephp')
))));

This approach will return Posts 1, 2 and 3. but it will show a blank
[Tag] array for Post #2 which is no good for me, since I don't want
the Post to show up at all.

The second approach was to use join tables (inner joins), using unbind
and bindModel, as detailed by teknoid in his blog (http://
teknoid.wordpress.com/2008/07/17/forcing-an-sql-join-in-cakephp/)

$this->Post->unbindModel(array('hasAndBelongsToMany'=>array('Tag')));

$this->Post->bindModel(array('hasOne'=>array(
        'PostsTag'=>array(
         'foreignKey'=>false,
          'type'=>'INNER',
           'conditions'=>array('PostsTag.Post_id = Post.id')
        ),
        'Tag'=>array(
          'className' => 'Tag',
      'foreignKey'=>false,
      'type'=>'INNER',
      'conditions'=>array(
      'Tag.id = PostsTag.Tag_id'
    )))));

$this->Post->find('all', array('
        'not' => array('Tag.name' => 'cakephp' ))));

This case almost provides me with the array I want (or at least one
that I can use), except for the part that Post #3 will appear twice.
One with the 'cool' tag as the result and one with the 'dessert' tag
as the result. Also, in this case, the Tags do not nest inside of the
[Tag] section of the array, which is desirable.

So, my question is: is there a way to tweak any of these two methods
to get the result I want??

I tried to provide as many details as I could. If i didn't make myself
clear, please let me know.

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

Reply via email to