Welcome Guest, Not a member yet? Register   Sign In
Active Record Join Missing Base Table
#1

[eluser]praj[/eluser]
Hi,

I'm on CodeIgniter 2.02, and curious about the SQL generated with the following code in the "file_model.php" that I've created:

Code:
$this->db->join('users', 'files.username = users.username', 'inner');        
$this->db->where('files.deleted', 0);
$this->db->order_by('files.username');            
$q = $this->db->get('files');                
echo $this->db->last_query();

The last line displays the select statement produced:

Code:
SELECT *
FROM (`files`) INNER JOIN `users`
ON `files`.`username` = `users`.`username`
WHERE `files`.`deleted` = 0
ORDER BY `files`.`username`

Why is the files table in brackets -- (`files`)?

I would have expected the SQL to look like this (without the brackets):

Code:
SELECT *
FROM `files` INNER JOIN `users`
ON `files`.`username` = `users`.`username`
WHERE `files`.`deleted` = 0
ORDER BY `files`.`username`
#2

[eluser]danmontgomery[/eluser]
https://bitbucket.org/ellislab/codeignit...php#cl-516

Code:
/**
* From Tables
*
* This function implicitly groups FROM tables so there is no confusion
* about operator precedence in harmony with SQL standards
*
* @access    public
* @param    type
* @return    type
*/
function _from_tables($tables)
{
    if ( ! is_array($tables))
    {
        $tables = array($tables);
    }

    return '('.implode(', ', $tables).')';
}
#3

[eluser]praj[/eluser]
Makes sense, thanks!




Theme © iAndrew 2016 - Forum software by © MyBB