Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to Dynamically Set table name in the Model fuelphp
  • I have problem when I want to set table name with my prefix from my config file.

    class Model_Users extends \Orm\Model
    {
    protected static $_table_name ;
            
            public function before() 
            {
                parent::before();

                self::$_table_name = \Config::get('dbase.db.prefix').'users';

            }
    }

    and when I am run I got the error

    42000! 

    I think that error because table_name is empty or not set.

    any suggestion ?
  • HarroHarro
    Accepted Answer
    You never use self (unless in very specific cases), you use static.

    Having said that, a before() method is something controllers only have. That doesn't work for other classes. For static data, you can do:

    public static function _init()
    {
        static::$_table_name = \Config::get('dbase.db.prefix').'users';
    }

    and having said that: you know you can define a table prefix in your db.php config file?
  • Thanks for support,

    Hola You Right, I think it's will be nice if I am define in db.php

Howdy, Stranger!

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

In this Discussion