Welcome Guest, Not a member yet? Register   Sign In
Datamaper ORM complex count on related table
#1

[eluser]elmystica[/eluser]
For the first time, I'm using a datamapper for a CodeIgniter project.

I have a table products, in which I store my items. There is a table categories, that specifies the kind of item. And a table brands, so we know which brand a product item belongs to.
There are join tables for brands_products and categories_products.

I want to be able to know how many products we have for each brand in a specific category. It would result in something like this:

Category A:
- brand 1 (6 products)
- brand 2 (4 products)
- brand 3 (7 products)
I started with something like this:

Code:
$products = new Product();

$products->where_related_category('url', $this->uri->segment(3))
     ->order_by('brands.name')
     ->include_related('brand', array('id', 'name', 'url'), true, true)
     ->include_count_related(null, 'brand')
     ->get();

But of course, this doesn't make the count work. I'm kind of blinded to see te obvious solution, I guess ... Any ideas?




Theme © iAndrew 2016 - Forum software by © MyBB