I don't think you will have much luck getting cake to produce a
similar query.

But an easy solution is to wrap your query inside a function in your
Product model.  Say

function getCategoryProducts($categoryId)
{
  return $this->query(/*that query you've written*/);
}

Then you can use that in your controllers:

$catProds = $this->Product->getCategoryProducts($categoryId);

Just be sure to use table aliases like "FROM products AS Product" to
make the array nicer.


Russell Austin

On May 8, 3:04 pm, johnvv <[EMAIL PROTECTED]> wrote:
> I'm to new using cake so file this question under 'newbies'...
>
> I have the following models:
> - Product (contains reference to manufacturer_id... and other
> information)
> - ProductImage (id,product_id,type [thumbnail,medium,large],
> image_url)
> - Manufacturer
> - Category
> - CategoryProduct (category_id, product_id)
>
> Each Category has many Products
> Each Product has 3 ProductImages (thumbnail, medium, large) - maybe
> could be 3 separate "hasOne" associations with conditions for each
> type
> Each Product has 0 or 1 Manufacturer
>
> I'd like to minimize the number of queries when viewing the products
> in a category.
> I've tried many different associations but each time cake has
> generated separate queries to obtain the images and manufacturers.
>
> Without cake, this can be accomplished with 1 query.  It would look
> something like this:
> "SELECT product.*,product_image.*,manufacturer.*
> FROM category_product
> INNER JOIN category ON category.category_id =
> category_product.category_id
> INNER JOIN product ON products.product_id =
> category_product.product_id
> INNER JOIN product_image ON product_image.product_id =
> product.product_id AND product_image.type = 'thumbnail'
> LEFT JOIN manufacturer ON manufacturer.manufacturer_id =
> product.manufacturer_id
> WHERE category_product.category_id = $id
> ORDER BY $sort
> LIMIT $start, $limit
>
> But everything I have tried in cake has resulted in 40-some queries to
> accomplish the same result.
> Any ideas?


--~--~---------~--~----~------------~-------~--~----~
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