开发者

CakePHP: multi-deep model queries

开发者 https://www.devze.com 2023-03-24 09:42 出处:网络
I have the following models: Book -> hasOne(Subcategory) -> belongsTo(Category) I have to make the following query:

I have the following models:

Book -> hasOne(Subcategory) -> belongsTo(Category)

I have to make the following query:

select c.ds_category,count(*) from categories c,subcategories sc,books b
where b.id_subcategory=sc.id_subcategory and sc.id_category=c.id_category
group by c.ds_category

In the Book model I've defined

var $hasOne="Subcategory";

In the subcategories model I've defined

var $belongsTo="Category";

The problem is that when I call

return $this-&g开发者_高级运维t;find("all");

I cannot get categories table so cannot make group by. Ive tried to define $recursive=2 but I did not run. Can anyone help me in finding a way to get also Categories items?


shouldn't Book belongsTo Subcategory? And you should use Containable to have a more control over find.

0

精彩评论

暂无评论...
验证码 换一张
取 消