fuel/orm — Issue #123: Orm "Belongs To" relationship not working as expected
Author: bencorlett
Created: 2011-11-18 01:30:53+00:00
State: open

Hi,

I've got a controller, where I do this:

$orders = \Sales\Model_Order::find('all', array(
'related' => array(
'area' => array(
'where' => array(
array('key', 'ordersin/awaitingconfirmation'),
),
),
),
));

And I get this error:

SELECT `t0`.`id` AS `t0_c0`,
`t0`.`area_id` AS `t0_c1`,
`t0`.`increment_id` AS `t0_c2`,
`t0`.`first_name` AS `t0_c3`,
`t0`.`last_name` AS `t0_c4`,
`t0`.`email` AS `t0_c5`,
`t0`.`shipping_amount` AS `t0_c6`,
`t0`.`grand_total` AS `t0_c7`,
`t0`.`payment_method` AS `t0_c8`,
`t0`.`payment_description` AS `t0_c9`,
`t0`.`shipping_method` AS `t0_c10`,
`t0`.`shipping_description` AS `t0_c11`,
`t0`.`order_created_at` AS `t0_c12`
`t0`.`created_at` AS `t0_c13`,
`t0`.`updated_at` AS `t0_c14`,
`t1`.`id` AS `t1_c0`,
`t1`.`department_id` AS `t1_c1`,
`t1`.`key` AS `t1_c2`,
`t1`.`action` AS `t1_c3`,
`t1`.`name` AS `t1_c4`,
`t1`.`sort` AS `t1_c5`

FROM `sales_orders` AS `t0`
LEFT JOIN `department_areas` AS `t1` ON (`t0`.`area_id` = `t1`.`id`)
WHERE `t0`.`key` = 'ordersin/awaitingconfirmation'

(Also see [this screenshot](http://d.pr/Pc8X)). It is trying to do the "where" clause on the original table, not the relationship?

This is my Order model:

protected static $_belongs_to = array(
'area' => array(
'model_to' => 'Departments\\Model_Area',
'cascade_save' => false,
),
);

(Also see [this screenshot](http://d.pr/9oaK)).

As you see it belongs to an area.

And my Area model:

protected static $_has_many = array(
'orders' => array(
'model_to' => 'Sales\\Model_Order',
'cascade_save' => false,
),
);

(Also see [this screenshot](http://d.pr/pjvO)).

And the fields all exist in the database, see [orders](http://d.pr/icv3) and [areas](http://d.pr/iOcw).

Has anyone else run into this issue?

Thanks,

Ben