Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM and dynamic table names
  • hello everyone. I have an issue that I have been struggling for a few hours with now and cant get my head around. I am creating an app with fuel that will be using table names related to the users company id created during registration so <code>sohq_14_projects</code> the sohq is the site wide prefix, but the 14 is specific to that users company and only available once the user has logged in. I have 5 or so tables like this as well as some system wide tables which are just <code>sohq_users</code>. my projects model looks like this... <code>
    namespace Projects; class Model_Projects extends \Orm\Model
    { protected static $table_name = 'projects'; protected static $_primary_key = array('id'); protected static $_properties = array(
    'id',
    'project_title',
    'project_slug',
    'project_description',
    'project_users',
    'active',
    'archived',
    'deleted',
    'created_at',
    'updated_at'
    ); protected static $_observers = array(
    'Orm\Observer_CreatedAt' => array(
    'events' => array('before_insert'),
    'mysql_timestamp' => false,
    ),
    'Orm\Observer_UpdatedAt' => array(
    'events' => array('before_save'),
    'mysql_timestamp' => false,
    ),
    );
    }
    </code> I've tried adding a __construct() in there to alter the $table_name var but it doesnt work, ive tried reading through the Core ORM Model class to see if there are any setters that could help, Ive read all the ORM stuff in the docs and (although very helpful for my general understanding) nothing seems to say how you might use a use a different table name like: <code>Model_Projects()->forge(array('table_name' => \Config::get('db.default.table_prefix') . $company_id . '_projects'))-></code> or even
    <code>$model = new Model_Projects(array('table_name' => \Config::get('db.default.table_prefix') . $company_id . '_projects'));</code> any help would be awesome as im well stuck. :-\
  • Moved to the ORM forum.
  • oops, sorry about that :)
  • For anyone else that wants to change their table names at runtime, in your Model_Something define a variable called $_table_name and set it to public static <code>public static $_table_name = 'your_table_name'; </code> then when instantiating the Model_Something() class you can do:
    <code>
    $p = new Model_Something();
    $p::$_table_name = 'another_table_name';
    </code>

Howdy, Stranger!

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

In this Discussion