Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Database prefix problem
  • I have problem in Database class.
    Database custom select and from as table_prefix problem.
    $data = \DB::select_array(\Dev10\Core::get_fields_from_table('setting', 's.', array(), array())
    )->from(array('setting', 's'))->where('s.key', 'title')->execute();

    Query : SELECT `dev10_s`.`setting_id`, `dev10_s`.`group`, `dev10_s`.`key`, `dev10_s`.`value` FROM `dev10_setting` AS `dev10_s` WHERE `dev10_s`.`key` = 'title'

    \Dev10\Core::get_fields_from_table function return values array('s.group', 's.key', 's.value');

    Why dev10_s added prefix select and where variables?
    only queries s.key = 'title'...
    searched could not figure out...

    Old Framework Codeigniter...
  • HarroHarro
    Accepted Answer
    Do you have a prefix configured in your db.php config file? If so it will be used to prefix all table names.
  • Yes use prefix.
    Not living with the problem that when I use Codeigniter Active Record.
  • HarroHarro
    Accepted Answer
    I don't understand what you mean?

    You have defined a table prefix in your configuration, and are now suprised that all your tables are prefixed?
  • my all tables have a prefix.

    None PHP Query : SELECT `s`.`setting_id`, `s`.`group`, `s`.`key`, `s`.`value` FROM `dev10_setting` AS `s` WHERE `s`.`key` = 'title'

    Database Query : SELECT `dev10_s`.`setting_id`, `dev10_s`.`group`, `dev10_s`.`key`, `dev10_s`.`value` FROM `dev10_setting` AS `dev10_s` WHERE `dev10_s`.`key` = 'title'

    Database/connection.php edited lines;

    quote_table function;
    line 531 : $value[1] = $this->table_prefix().$value[1]; // original
    $value[1] = $value[1]; // new

    quote_identifier function;
    line : 668;
    if ($prefix = $this->table_prefix())
    {
    // Get the offset of the table name, 2nd-to-last part
    // This works for databases that can have 3 identifiers (Postgre)
    $offset = count($parts) - 2;

    // Add the table prefix to the table name
    // $parts[$offset] = $prefix.$parts[$offset];
    $parts[$offset] = $prefix.$parts[$offset];
    }

    /*
    if ($prefix = $this->table_prefix())
    {
    // Get the offset of the table name, 2nd-to-last part
    // This works for databases that can have 3 identifiers (Postgre)
    $offset = count($parts) - 2;

    // Add the table prefix to the table name
    // $parts[$offset] = $prefix.$parts[$offset];
    $parts[$offset] = $prefix.$parts[$offset];
    }
    */

    Database/connection.php after; 
    SELECT `s`.`setting_id`, `s`.`group`, `s`.`key`, `s`.`value` FROM `dev10_setting` AS `s` WHERE `s`.`key` = 'title'

    File modified Problem Solved...
  • HarroHarro
    Accepted Answer
    Ah, your problem is that the alias is prefixed too.

    That's only a cosmetic issue, but if it bothers you, please create an issue for it at https://github.com/fuel/core/issues, linking to this thread. Or send in a PR with the fix.
  • I've looked into it, but although it might solve your particular problem, this doesn't solve THE problem. It creates one.

    Issue is that if in quote_identifier() you get a string "this.that", can you tell that "this" is a table name (which should be prefixed) or an alias (which should not be prefixed)? Problem is, you can't, so the solution that was implemented is to prefix both.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion