JDatabase : Subquery in join left, then where clause

Did you find a bug in Joomla! 3.x but aren't sure? This forum is the place to help figure out if the problem is a bug and how to report it. If you are an experienced Joomla! user and are certain that you have found a bug please use the Bug Tracker to submit your issue.
This forum is for discussion about bugs and to get help with reporting them to the Bug Tracker: https://issues.joomla.org

Moderator: ooffick

Forum rules
Please use the official Bug Tracker to report a bug: https://issues.joomla.org
Locked
LouisSeb
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Wed Sep 14, 2016 10:45 am

JDatabase : Subquery in join left, then where clause

Post by LouisSeb » Wed Sep 14, 2016 11:01 am

Summary: when using a subrequest for a join statement, the "where" clause after the join are not taken in account.

Two table:
- j_ls_installations (list of items)
- j_users_ls_installations (relation between item and users, based on ls_id )

The query must retrieves all the items ("installations") of a user, then for each item, count how many other users can access to it.

Here a working pure MySQL working code. It:

Code: Select all

    SELECT DISTINCT installations. * , users.ls_role, t.team
    FROM j_ls_installations AS installations
    LEFT JOIN j_users_ls_installations AS lsusers ON installations.ls_id = lsusers.ls_id
    LEFT JOIN (
        SELECT  `ls_id` , COUNT( * ) as team
        FROM  `j_users_ls_installations`
        GROUP BY  `ls_id`
    ) AS t ON installations.ls_id = t.ls_id
    WHERE lsusers.ls_user_id =269


Here the JDatabase version of it:

Code: Select all

        $query = $db->getQuery(true);
        $subQuery = $db->getQuery(true);
        
        $subQuery->select($db->quoteName('ls_id').', COUNT(*) as team')
            ->from($db->quoteName('#__users_ls_installations'))
            ->group($db->quote('ls_id'));

        // Create the base select statement.
        $query
            ->select(
                'DISTINCT installations.*,  lsusers.ls_role, t.team'
                )
            ->from($db->quoteName('#__ls_installations').' as installations')
            ->leftJoin($db->quoteName('#__users_ls_installations').' as lsusers on installations.ls_id=lsusers.ls_id')
            ->leftJoin( '('. $subQuery .') as t on installations.ls_id = t.ls_id')
            ->where($db->quoteName('lsusers.ls_user_id') . ' = ' . $oUser->id);
Here the generated code:

Code: Select all

  SELECT DISTINCT installations.*,  lsusers.ls_role, t.team 
  FROM `j_ls_installations` as installations 
  LEFT JOIN `j_users_ls_installations` as lsusers 
     on installations.ls_id=lsusers.ls_id 
  LEFT JOIN ( SELECT `ls_id`, COUNT(*) as team 
  FROM `jkg9e_users_ls_installations` 
  GROUP BY 'ls_id') as t 
  on installations.ls_id = t.ls_id
As you can see, the generate method don't have the last "where" statement, it just stop after joining the subrequest.


(let me know if you want me to share the component, to get the tables and everything and test it. )

LouisSeb
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Wed Sep 14, 2016 10:45 am

Re: JDatabase : Subquery in join left, then where clause

Post by LouisSeb » Wed Sep 14, 2016 11:10 am

Ok, adding an alias in the subquery solved the problem:

Code: Select all

        $query = $db->getQuery(true);
        $subQuery = $db->getQuery(true);
        
        $subQuery->select($db->quoteName('ls_id').', COUNT(*) as team')
            ->from($db->quoteName('#__users_ls_installations'). ' as b')
            ->group($db->quote('ls_id'));

        // Create the base select statement.
        $query
            ->select(
                'DISTINCT installations.*,  lsusers.ls_role, t.team'
                )
            ->from($db->quoteName('#__ls_installations').' as installations')
            ->leftJoin($db->quoteName('#__users_ls_installations').' as lsusers on installations.ls_id=lsusers.ls_id')
            ->leftJoin( '('. $subQuery .') as t on installations.ls_id = t.ls_id')
            ->where($db->quoteName('lsusers.ls_user_id') . ' = ' . $oUser->id);
I just added 'as b':

Code: Select all

            ->from($db->quoteName('#__users_ls_installations'). ' as b')


Locked

Return to “Joomla! 3.x Bug Reporting”