Quantcast

Find with $hasAndBelongsToMany

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Find with $hasAndBelongsToMany

Hill180
I have three models 
Authors 
Books (hasandbelongstomany category)
Category

They are properly configured for $hasAndBelongsToMany (books/category) relationship.

I need to get all books by an author in a certain category (5 in this example)

Obviously the category id is not in the authors table so I am trying to figure out how to reference the category id.  I have tried below to no avail.

$this->Category->Author->find('all', array('conditions'=>array('Category.id'=>'5', 'Author.id'=>'2')));
or 
$this->Author->find('all', array('conditions'=>array('Category.id'=>'5', 'Author.id'=>'2')));

This is the first time I have done a HABTM query.  (searched manual and and a couple cake books - no help)

Thanks!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Find with $hasAndBelongsToMany

Shaz-3
I've just posted a similiar query!

Googling around has given me the bindModel() method - which you can
read at the bottom of the page here:

http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM

But I wasn't able to get it to work for me. There's also a method
using manual joins and creating models for the join table and querying
it - haven't tried either of the two and can't say if they'll work.

On May 30, 5:14 pm, hill180 <[hidden email]> wrote:

> I have three models
> Authors
> Books (hasandbelongstomany category)
> Category
>
> They are properly configured for $hasAndBelongsToMany (books/category)
> relationship.
>
> I need to get all books by an author in a certain category (5 in this
> example)
>
> Obviously the category id is not in the authors table so I am trying to
> figure out how to reference the category id.  I have tried below to no
> avail.
>
> $this->Category->Author->find('all',
> array('conditions'=>array('Category.id'=>'5', 'Author.id'=>'2')));
> or
> $this->Author->find('all', array('conditions'=>array('Category.id'=>'5',
> 'Author.id'=>'2')));
>
> This is the first time I have done a HABTM query.  (searched manual and and
> a couple cake books - no help)
>
> Thanks!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Find with $hasAndBelongsToMany

Hill180
I saw that page in the manual, but glanced over the answer.  Thanks!

Shaz, this is what I did to solve the problem if you are still having problems.

$this->Author->bindModel(array(
'hasAndBelongsToMany' => array(
'Category' => array('conditions'=>array('Category.id'=>'5'))
)));
$this->Author->find('all', array('conditions'=>array('Author.id'=>'2')));

On Mon, May 30, 2011 at 1:06 PM, Shaz <[hidden email]> wrote:
I've just posted a similiar query!

Googling around has given me the bindModel() method - which you can
read at the bottom of the page here:

http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM

But I wasn't able to get it to work for me. There's also a method
using manual joins and creating models for the join table and querying
it - haven't tried either of the two and can't say if they'll work.

On May 30, 5:14 pm, hill180 <[hidden email]> wrote:
> I have three models
> Authors
> Books (hasandbelongstomany category)
> Category
>
> They are properly configured for $hasAndBelongsToMany (books/category)
> relationship.
>
> I need to get all books by an author in a certain category (5 in this
> example)
>
> Obviously the category id is not in the authors table so I am trying to
> figure out how to reference the category id.  I have tried below to no
> avail.
>
> $this->Category->Author->find('all',
> array('conditions'=>array('Category.id'=>'5', 'Author.id'=>'2')));
> or
> $this->Author->find('all', array('conditions'=>array('Category.id'=>'5',
> 'Author.id'=>'2')));
>
> This is the first time I have done a HABTM query.  (searched manual and and
> a couple cake books - no help)
>
> Thanks!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Loading...