Welcome Guest, Not a member yet? Register   Sign In
CI4 Alias not working
#1

(This post was last modified: 09-06-2023, 08:37 AM by Gumzy.)

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',
    ]; 
Reply
#2

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...-setprefix
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

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

"flare_" prefix. He auto concat with table
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

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.
Reply
#6

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/CodeIgni...#L295-L304
Reply
#7

(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...-setprefix

Apperciate it.

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




Theme © iAndrew 2016 - Forum software by © MyBB