[eluser]elmystica[/eluser]
* Bump
I have three tables:
-
products with an id (PK), name, etc ...
-
brands with an id (PK), brand_label, etc ..
-
brands_products with an id (PK), product_id, brand_id
My product -method:
Code:
<?php
class Product extends DataMapper {
var $table = 'products';
var $has_one = array(
'brand' => array(),
'category' => array(),
'gender' => array(),
'material' => array(),
'shape' => array(),
'structure' => array()
);
var $has_many = array(
'color' => array(),
'list_item' => array()
);
function __construct($id = NULL)
{
parent::__construct($id);
}
}
My Brand Method:
Code:
<?php
class Brand extends DataMapper {
var $has_many = array('product');
function __construct($id = NULL)
{
parent::__construct($id);
}
}
In my controller, I want to select ALL products where "brand.brand_url" is a certain brand passed along in the URL.
Code:
$product = new Product();
$product->where_related_brand('brand_url', $this->uri->segment(3))->include_related('brand', array('id', 'brand_label', 'brand_url', 'brand_lc'), FALSE, TRUE)->get_iterated();
echo $this->db->last_query() . "<br />";
echo "=====================<br />";
foreach($product as $item)
{
echo "<br />" . $item->brand->brand_label . ": brilnr. " . $item->name;
}
echo "=====================<br />";
It gives me a query like this:
Code:
SELECT products.* FROM products LEFT OUTER JOIN brands_products brands_products ON products.id = brands_products.product_id LEFT OUTER JOIN brands brands ON brands.id = brands_products.brand_id WHERE brands.brand_url = 'gucci'
and it returns only one result ...
As this is something I will do often, what is the right way to get the correct joins within Datamapper?