[eluser]elmystica[/eluser]
hi,
I'm having two tables: products and brands.
Products:
Code:
id
name
brand_id
...
Brands
Every product belongs to only one brand ofcourse, but brands have many products.
I don't want to use a join-table (I use them on a lot of other relations).
I have two models:
Products
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);
}
}
Brands
Code:
<?php
class Brand extends DataMapper {
var $has_many = array(
'product' => array(
'class' => 'product',
'other_field' => 'product',
'join_table' => ''
)
);
function __construct($id = NULL)
{
parent::__construct($id);
}
}
I made this part in a controller:
Code:
$p = new Product();
$p->get_by_id($this->uri->segment(3));
$p->brand->get();
echo $this->db->last_query() . "<br />";
foreach($p->brand as $b)
{
echo $b->brand_label;
}
The goal is to detect the brand on a product.
However, this doesn't work as I hoped it would ...
I noticed the query was like this:
Code:
SELECT brands.* FROM brands LEFT OUTER JOIN brands_products brands_products ON brands.id = brands_products.product_id WHERE brands_products.product_id = 5
it relies on join-table, which I don't want to use.
What is the right way to do this kind of operations?