Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How To Set Table Name Dinamic?
  • (1) I have many schedule tables.

    tbl_schedule201306
    tbl_schedule201305
    tbl_schedule201304....

    (2) I want to SELECT them by ORM.

    $schedules = \Schedule\Model_Schedule::find()
        ->related( array('user') )
        ->where('user.id', '=', $user_id)
        ->get();


    Can I set "$_table_name" property dinamic?
    e.g. $_table_name = 'schedule' . $target_date . 's';

    (It's STATIC, I think It's unpossible. But I like so fuelphp's ORM. It's cool!)

  • HarroHarro
    Accepted Answer
    Yes, why not?

    It's a protected property, so you need to define a method for it:

    public static function set_table($table)
    {
        static::$_table_name = $table;
    }
  • Oh, cool. thank you a lot.
  • I set $_table_name dynamically.
    But, new trouble happen.

    -------------------------------------------------------
    <Model>
    -------------------------------------------------------
    public static function set_table($table)
    {
        static::$_table_name = $table;
     
       // Retuen self instance. 
       // Because, continue methods (e.g. " ->where(...)->get() " in controller.
       return new self;
       
    }

    -------------------------------------------------------
    <Controller>
    -------------------------------------------------------
    (1) SELECT
    $schedules = Model_Schedule::set_table('201306')->get();

    Work fine:)

    (2) INSERT
    $schedules = Model_Schedule::set_table('201306')->forge( array( 'title' => $title, 'datetime' => $datetime ) );

    Error.

    What is best way to set table name to Model Dynamically?

  • In general, never use "new" on Fuel classes, always use forge(). Sometimes "new" works, but often is has side-effects you really don't want.

    It's going to be complex to do this dynamically. ORM has been designed with a one-to-one mapping between a Model and a table, which is why it's defined static. If you change the static table name, you will change if will all already existing instances of that Model too, which could lead to severe problems.

    Second issue you have is that table names are cached. Everything in ORM uses the table() method to retrieve the table name linked to a Model class from cache, it will not use the $_table_name property. So changing this property at runtime has no effect at all.

    The best option is probably to make an $_overloaded_table_name property, and have table() check if this is defined, and if so, return that value instead of the value from cache.
  • Thank you.
    I understand.
    I'll try make an $_overloaded_table_name property property.
    I think...
    ---------------------
    <Model>
    ---------------------
    class Model_Schedule extends \Orm\Model_Soft {

        protected static $_overloaded_table_name = '';

        public static function set_table($table)
        {
            static::$_overloaded_table_name = $table;
        }

    }
    ---------------------
    <Controller>
    ---------------------
    $table_name = 'tbl_schedule_201306s';
    $schedules = \Model_Schedule::set_table_name( $table_schedule );
    $schedule->table();

    How refrect to this property for table() method?
    \DB Class is simpler?
  • HarroHarro
    Accepted Answer
    You only want to use a single table for this model? Or different tables at the same time? In that last case, you can't use a static solution.

    If you can use a static property, add this to your model:

        public static function table()
        {
            if (empty(static::$_overloaded_table_name))
            {
                return parent::table();
            }
            else
            {
                return static::$_overloaded_table_name;
            }
        }

  • Thank you.
    I use different tables.
    I cannot use static method this case.
    But I'll use you wrote this model code when using a single table! 

Howdy, Stranger!

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

In this Discussion