On Dec 19, 12:44 pm, "Jon Bennett" <[EMAIL PROTECTED]> wrote:
> Anyone has suggestions for this on? I'm having similar problems.hmm, I don't 
think you can with a vanilla HABTM, you need to fake it
by creating a hasMany and belongsTo association with a JoinTable
model, check out:

http://www.thinkingphp.org/2006/10/26/modeling-relationships-in-cakep...

hth

jb

--

jon bennett
t: +44 (0) 1225 341 039 w:http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

Hi All,

With a bit of trickery, this problem is not very difficult to solve. If
the following explanation isn't sufficient I'll put some examples on my
blog (or maybe I'll do that anyway).

To perform a search with a constraint in 2 models(tables) you need the
two tables to be JOINed together. Key fact to bear in mind: JOINs only
appear in the sql for hasOne or belongsTo associations.

For a HABTM you would/could do the following:
1) Create a model for the join table.
2) bindModel a dummy hasOne association for the join table before you
search
3) use a constraint that refers to the main model and the join
table(not the other model)

Working E.g.
$this->Post->bindModel(array('hasOne'=>array('PostsTag'=>array())));
$constraint['Post.published'] = '< '.date('Y-m-d H:i:s');
$constraint['PostsTag.tag_id'] = $tagId; // or pass an array to
genereate "IN (1,2,3..)" in the sql.
$this->Post->findAll($constraint);

1 caveat:
If you get, or are at risk of getting, duplicate entries (IN(1,2,3..)),
add DISTINCT to the main models id field in the field list (2nd findAll
parameter) like so: $fields = array('DISTINCT id','body','published',
etc.)

HTH,

AD7six
Please note:
The manual/bakery is a good place to start any quest for info.
The cake search (at the time of writing) erroneously reports less/no
results for the google group.
The wiki may contain incorrect info - read at your own risk (it's
mainly user submitted) :) You may get your answer quicker by asking on
the IRC Channel (you can access it with just a browser
here:http://irc.cakephp.org).


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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