On Apr 3, 2:48 am, "jyrgen" <[EMAIL PROTECTED]> wrote:
> hi all,
>
> in this thread
>
> http://groups.google.com/group/cake-php/browse_thread/thread/f23b1825...
>
> Andy Dawson gave valuable information for searching HABTM related
> models by
> creating a dummy model for the HABTM lookup table.
>
> > $constraint['PostsTag.tag_id'] = $tagId; // or pass an array to genereate 
> > "IN (1,2,3..)" in the sql.
>
> now here i want fields to be "AND-ed" while  "IN (1,2,3..)" generates
> "OR-ed" fields.
>
> i tried this:
>
> $conditions['ArticlesRight']['right_id'] = Array("and" => array(1,2));
> with the same result.
>
> and this:
>
> $conditions['ArticlesRight']['right_id'] = Array("and" =>
> array("ArticlesRight" => Array("right_id" => 1, "right_id" => 2)));
> which doesn't make sense, because the innermost Array has identical
> keys.
>
> how do i have to construct the array in order to achieve AND logic ?
>
> thank you,
>
> jyrgen

/me flicks his burning ear.

Hi jyrgen,

Did you and I discuss this on irc recently..?

While the result of what you want is clear (at least to me), the way
it is described here cannot solve the problem. Changing the IN to be
an AND is illogical - the field in A row in the database can only have
ONE value.

It's important to know or at least understand the sql that will give
you what you want.

// Find all posts
SELECT * FROM posts;

// Find all posts linked to the tag with id 1
SELECT *
FROM   posts
       LEFT JOIN posts_tags
         ON posts_tags.post_id = posts.id
WHERE  posts_tags.tag_id = 1

// Find all post linked to both tag 1 AND tag 2
// Note the increase in complexity
SELECT *
FROM   posts
       LEFT JOIN posts_tags AS tag1
         ON tag1.post_id = posts.id
       LEFT JOIN posts_tags AS tag2
         ON tag2.post_id = posts.id
WHERE  tag1.tag_id = 1
       AND tag2.tag_id = 2

What you need is to bind a hasOne association for each of the joins
which appears in that last example. If you wanted to say "linked to
all 8 items", you would need to LEFT JOIN (or inkeeping with the
previous thread bind a hasOne for) 8 dummies to achieve the goal.

HTH,

AD


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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