fuel / orm

Fuel PHP Framework - Fuel v1.x ORM
http://fuelphp.com/docs/packages/orm/intro.html
151 stars 95 forks source link

Fixed WHERE condition doesn’t work when use getone in the soft_filter #395

Closed linzhengen closed 7 years ago

linzhengen commented 7 years ago

doesn’t work by these case

PHP logic

 // case 1
 Model_Project::find('first',
  array(
      'related' => array('project_users'),
      'where' => array(
          array('project_users.id', '=', 2),
          array('code', '=', 'code1'),
      ),
  ));

 // case 2
 Model_Project::query(
  array(
      'related' => array('project_users'),
      'where' => array(
          array('project_users.id', '=', 2),
          array('code', '=', 'code1'),
      ),
  ))->get_one();

 // case 3
 Model_Project::query()
    ->related('project_users')
    ->where('project_users.id', '=', 2),
    ->where('code', '=', 'code1'),
    ->get_one();

generated the same SQL The t0 subquery doesn’t work, because it don't have any where condition for limit 1

SELECT 
  `t0`.`id` AS `t0_c0`, 
  `t0`.`code` AS `t0_c1`, 
  `t0`.`deleted_at` AS `t0_c2`, 
  `t0`.`created_at` AS `t0_c3`, 
  `t0`.`updated_at` AS `t0_c4`, 
  `t1`.`id` AS `t1_c0`, 
  `t1`.`project_id` AS `t1_c1`, 
  `t1`.`user_id` AS `t1_c2`, 
  `t1`.`deleted_at` AS `t1_c3`, 
  `t1`.`created_at` AS `t1_c4`, 
  `t1`.`updated_at` AS `t1_c5` 
FROM 
  (
    SELECT 
      `t0`.`id`, 
      `t0`.`code`, 
      `t0`.`deleted_at`, 
      `t0`.`created_at`, 
      `t0`.`updated_at` 
    FROM 
      `projects` AS `t0` 
    WHERE 
      `t0`.`deleted_at` IS null 
    ORDER BY 
      `t0`.`id` ASC 
    LIMIT 
      1
  ) AS `t0` 
  LEFT JOIN `project_users` AS `t1` ON (
    `t0`.`id` = `t1`.`project_id` 
    AND `t1`.`deleted_at` IS NULL
  ) 
WHERE 
  (
    `t1`.`id` = 2 
    AND `t0`.`code` = 'code1' 
  ) 
ORDER BY 
  `t0`.`id` ASC

But it works by these case

// case 1
 Model_Project::find('first',
     array(
         'related' => array('project_users' => array(
             'where' => array(array('id', '=', 2))
         )),
         'where' => array(
             array('code', 'code1'),
         ),
     )
 );
// case 2
 Model_Project::query(
     array(
         'related' => array('project_users' => array(
             'where' => array(array('id', '=', 2))
         )),
         'where' => array(
             array('code', 'code1'),
         ),
     )
 ))->get_one();
WanWizard commented 7 years ago

We don't accept PR's on release branches, only on the latest develop branch.

And the ORM is not a query builder. This behaviour is by design, if you select objects with relation, it will apply the limit on the parent, but it always fetch all children, otherwise the resultset is not consistent. The ORM will never select partial relation data.

If you want your behaviour, you need to use 'rows_limit' instead of 'limit'. This behaviour is documented.

linzhengen commented 7 years ago

Thank you for your comment.