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,
  • Harro VertonHarro Verton
    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';
    }
  • Harro VertonHarro Verton
    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 .

Howdy, Stranger!

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

In this Discussion