Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
I can not dynamically change table name(dev-1.9)
  • Hello there,

    I have following method in the model.
     
     public static function set_table(int $user_id)                                                                                
      {                                                                                                                             
        static::$_table_name = "tests_".substr(str_pad((string)$user_id,2,0,STR_PAD_LEFT),-2); 
      }   

    public static function add($user_id)
    {
      $prop = array('user_id'=>$user_id);
      self::set_table($user_id);
      return self::forge($prop)->save(); 
    }

    But, it dose not change the table name.

    Do you have any suggetion for this?

    Regards,
  • HarroHarro
    Accepted Answer
    Do not use self:, use static:. Late static binding is important if you change static properties.
  • in your model:
    public static function table()
    {
          // some magic code
          return 'new_table_name';
    }
  • HarroHarro
    Accepted Answer
    The model already has a static method for retrieving the table name.

    The problem is that self:: will call the original base model, not the current derived class. You need late static binding for that.
  • Hello there,

    Sorry for late reply.

    Thank you very much for your help.

    I did not know that I have to use static:: all the time when I change static properties.

    I still have to more about PHP.

    Thank you, Harro :)
  • Thank you, Harror :)
    I always appreciate it .
  • Hello there,

    I have different issue about this.

    When I try to change the table like following, it dose not save the data that I specified.

    public static function set_table(int $user_id)                                                                                
      {                                                                                                                             
        static::$_table_name = "tests_".substr(str_pad((string)$user_id,2,0,STR_PAD_LEFT),-2); 
      }   

    public static function add($user_id)
    {
      $prop = array('user_id'=>$user_id);
      static::set_table($user_id);
      return static::forge($prop)->save(); 
    }
  • I can see that the table is changed when I did "echo static::$_table_name".
    But, when I continue to keep changing the table name and save the data, somehow the previous table name is still there, not new table name.

    For example, when I intend to change the table like following.

    table_01
    table_02
    table_03
    table_04
    table_05
    ...

    The data is saved to wrong table like following.
    table_01
    table_01
    table_02
    table_01
    table_03
    ...
    It looks like previous table name is still there when I save the data.
    Is there something wrong with my source code?
    Best regards,
  • HarroHarro
    Accepted Answer
    See the table() method.

    The table name is cached by classname in static::$_table_names_cached so other model instances can easily look it op.

Howdy, Stranger!

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

In this Discussion