A few ideas on how I usually solve this problem: 1) Fetching a record from the database automatically gets you all the related models as well, presuming Model::$recursive is high enough.
$this->Category->find("Category.id = $id") will get you an array that looks like this: array( [Category] => array(...), [Link] => array( /* all links in this category */ ) ) 2) If you need Links that are in Category 1, 2 or 3, either do the above and pick all the Link entries you need (Set::extract() works great), or, depending on how interconnected your models are and what you need to fetch: $categories = $this->Category->find('all', array('conditions' => array('id' => array(1, 2, 3)))); $link_ids = Set::extract('/Link/id', $categories); // link_ids now holds all ids of all Links in Categories 1, 2 and 3 $this->Link->find('all', array('conditions' => array('Link.id' => $link_ids))); Take your pick, limit recursiveness and/or containability as you see fit to limit the overhead. Chrs, Dav On 2 Sep 2008, at 15:43, Predominant wrote: > > Hey David, > > I was wondering if you could drop me the SQL for your database (just > the tables involved), as well as the models and the final controller > code. > Currently I am dealing the the same example, only with differing model > names. I wanted to check with you regarding your database structure, > and see how you ended up with things. > > Very interested to hear from you. > > Cheers, > Graham > > > On Aug 22, 2:15 am, David Yell <[EMAIL PROTECTED]> wrote: >> I finally managed to get this working using a model bind. It's a bit >> messy and doesn't really make a whole lot of sense to me just yet. I >> was basically changing things and looking at the sql. For anyone >> looking for this type of functionality, I achieved it using this in >> my >> controller. >> >> <?php >> function index() { >> $this->checkRole(array(1,3)); >> >> $this->Link->recursive = 1; >> >> $this->Link->bindModel( >> array('belongsTo' => array( >> 'CategoriesLink' => array( >> 'className' >> => 'CategoriesLink', >> >> 'foreignKey' => 'id', >> ) >> )), >> array('belongsTo' => array( >> 'Category' => array( >> 'className' >> => 'Category', >> >> 'foreignKey' => 'categories_link_id', >> ) >> )) >> ); >> >> if(isset($this->params['named']['category'])){ >> $data = $this->paginate('Link', >> array('CategoriesLink.category_id'=> >> $this->params['named']['category'])); >> } else { >> $data = $this->paginate('Link', >> array('Link.status_id'=>'1')); >> } >> >> die(pr($data)); >> >> $this->set('links', $data_mod); >> >> $categories = $this->Link->Category->find('list'); >> $this->set(compact('categories'));} >> >> ?> >> >> On Aug 20, 12:34 pm, David Yell <[EMAIL PROTECTED]> wrote: >> >>> Does this mean that 1.2 still can't filter a result set by a related >>> model over habtm? >> >>> I've been trying to filter my Links by Category to no avail today. >> >>> Link habtm Category >>> Category habtm Link >> >>> $this->set('links', $this->paginate('Link', array('Category.id'=> >>> $this- >> >>>> data['Link']['category_id']))); >> >>> Which fails because CakePHP won't join the related model in the >>> query. >>> Will I be reduced to creating a Model for the join CategoriesLink >>> and >>> then writing my own query to grab data from it? It seems >>> determined to >>> do two separate queries. >> >>> The only other way I could think of, which I haven't tried yet, >>> was to >>> use /category/5/$link_id which might work, but I assumed would >>> suffer >>> the same fate in the sql of missing a join. Or should I be making an >>> on-the-fly bind using a belongsTo association to force the join, >>> which >>> would then pickup my conditions? >> >>> I have read the CakeBaker article (http://cakebaker.42dh.com/ >>> 2007/10/17/pagination-of-data-from-a-habtm-relationship/), but that >>> looks to be for 1.1 as my existing habtm works for reading and >>> saving, >>> but not for when you want to adjust the resultset using a field from >>> the related model. >> >>> Idea's or links would be appreciated! > > --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---