CodeIgniter-MY_Model icon indicating copy to clipboard operation
CodeIgniter-MY_Model copied to clipboard

Right Join instead of Left Join

Open erralb opened this issue 8 years ago • 0 comments

Hi,

I have a Role mode linked with a Subcategory model through a pivot table. In my Role model, I have :

    $this->has_many_pivot['subcategories'] = array(
        'foreign_model'=>'Subcategories_model',
        'pivot_table'=>'app_menus_app_userroles',
        'local_key'=>'id',
        'pivot_local_key'=>'app_menu_id',
        'pivot_foreign_key'=>'app_userrole_id', 
        'foreign_key'=>'id',
        'get_relate'=>TRUE 
    );

Then when I call :

        $role = $this->roles_model
                      ->with('subcategories')
                      ->get($r->id);

I get the following result :

stdClass Object (
[id] => 2
[name] => Client
[reference] => client
[subcategories] =>
)

print_r($this->db->last_query()); returns :

SELECT `app_menus`.`id`, `app_menus_app_userroles`.`app_menu_id`
FROM `app_menus`
LEFT JOIN `app_menus_app_userroles` ON `app_menus`.`id` = `app_menus_app_userroles`.`app_userrole_id`
LEFT JOIN `app_userroles` ON `app_menus_app_userroles`.`app_menu_id` = `app_userroles`.`id`
WHERE `app_menus_app_userroles`.`app_menu_id` IN('2')

Which also returns nothing when I run it on mysql. But when I replace the Left Joins with Right Joins in the query, I get the expected results (there's definitely subcategories linked with this role in my db)

What am I doing wrong here? Is my definition wrong or is there a way to force a right join instead of a left join?

Kind regards

erralb avatar Aug 14 '17 10:08 erralb