CodeIgniter Forums
CI4 Alias not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: CI4 Alias not working (/showthread.php?tid=88423)



CI4 Alias not working - Gumzy - 09-06-2023

Hi,

I cannot get my alias to work so that I can join other tables rather than running multiple queries at once.

PHP Code:
error:
Unknown column 'flare_l.id' in 'field list' 


Please see code below:

  
PHP Code:
      $builder $this->db->table($this->tables_alias['logs'])
            ->select('
                l.id as log_id,
                l.user,
                l.category,
                u.username
            '
)
        ->join($this->tables_alias['users'], 'l.user = u.id''LEFT'); 


PHP Code:
    public array $tables_alias = [
        'web'                      => 'system_settings ss',
        'meta'                      => 'meta_data md',
        'pages'                    => 'page_manager pm',
        'admin_logins'              => 'admin_logins al',
        'users'                    => 'user_accounts u',
        'role_users'                => 'role_users ru',
        'permission_parents'        => 'permission_parents pp',
        'role_permissions'          => 'role_permissions rp',
        'roles'                    => 'roles r',
        'permissions'              => 'permissions p',
        'logs'                      => 'logging l',
    ]; 



RE: CI4 Alias not working - ozornick - 09-06-2023

It seems table() only takes the table name and adds a prefix.
That's why you see the error.

If so important, try removing the prefix for the query. Perhaps there is another option
https://codeigniter4.github.io/userguide/database/queries.html#db-setprefix


RE: CI4 Alias not working - JustJohnQ - 09-06-2023

I don’t see flare_l.id in any of your code?


RE: CI4 Alias not working - ozornick - 09-06-2023

"flare_" prefix. He auto concat with table


RE: CI4 Alias not working - JustJohnQ - 09-06-2023

Looks like I can’t see the code on my phone. Can’t really tell why a table alias is necessary to create a join.


RE: CI4 Alias not working - kenjis - 09-06-2023

QueryBuilder's table name supports an alias.

Quote:@param array|string $tableName tablename or tablenames with or without aliases

Examples of $tableName: `mytable`, `jobs j`, `jobs j, users u`, `['jobs j','users u']`

See https://github.com/codeigniter4/CodeIgniter4/blob/be40bfb6230659cea93fd9a1c2ac181f7bb193df/system/Database/BaseBuilder.php#L295-L304


RE: CI4 Alias not working - Gumzy - 09-07-2023

(09-06-2023, 10:12 AM)ozornick Wrote: It seems table() only takes the table name and adds a prefix.
That's why you see the error.

If so important, try removing the prefix for the query. Perhaps there is another option
https://codeigniter4.github.io/userguide/database/queries.html#db-setprefix

Apperciate it.

The prefix was causing the issue so I've just removed it.