Hi,
I’m here again with my many to many relationship noobs question.
What is the best way to load join field when iterating on items.
Here are the models :
class Items extends DataMapper {
public $has_many = array(
'lang' => array(
'class' => 'lang',
'other_field' => 'item',
'join_self_as' => 'item',
'join_other_as' => 'lang', would be 'lang_id'
'join_table' => 'items_langs'
),
);
}
class Langs extends DataMapper {
public $has_many = array(
'item' => array(
'class' => 'item',
'other_field' => 'lang',
'join_self_as' => 'lang',
'join_other_as' => 'item', would be 'item_id'
'join_table' => 'items_langs'
),
);
}
Now I’m loading all items and looping on them. I want to display their data and the field “name” which is a data in the joined field (ie. in the table items_langs).
I have loaded my current language and I want to display the name of the item in my language. What is the best way to do it?
$items = new Item();
$items->get();
foreach($items as $item)
{
$item->lang->where('id', $default_language->id)->include_join_fields()->get();
echo $item->lang->join_name;
// Would be nice to pass related field as param like this :
$item->include_join_fields($default_language)->get();
}
Lang is already loaded why loading it again? Any better solution?