Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Orm
I can not dynamically change table name(dev-1.9)
soseki
January 2021
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
January 2021
Accepted Answer
Do not use self:, use static:. Late static binding is important if you change static properties.
atabak
January 2021
in your model:
public static function table()
{
// some magic code
return 'new_table_name';
}
Harro
January 2021
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.
soseki
February 2021
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
:)
Harro
February 2021
Accepted Answer
No worries, we've all been there.
See
https://www.php.net/manual/en/language.oop5.late-static-bindings.php
soseki
February 2021
Thank you, Harror
:)
I always appreciate it .
soseki
March 2021
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();
}
soseki
March 2021
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,
Harro
March 2021
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.
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,089
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
261
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
atabak
January 2021
Harro
March 2021
soseki
March 2021