Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Table Prefixes #19

Closed
igbanam opened this issue Sep 4, 2013 · 1 comment
Closed

Table Prefixes #19

igbanam opened this issue Sep 4, 2013 · 1 comment

Comments

@igbanam
Copy link

igbanam commented Sep 4, 2013

Feature Request
Is it possible to have table prefixes in the model name? Accomplishing this with the current release is done by putting individual names in the model file.

My Stack

  • CodeIgniter v2.1.4
  • php-activerecord -v0.0.2
  • [installed through] sparks v0.0.9

The use case
There are different domains of an enterprise application which share the same database.

For example, A company sells products handled by A company's website using the a_company database table. B company also sells products using their own website but shares database (say because of hosting restrictions) with A company. Assuming both follow the convention of naming their database models after the models—a_products and b_products, respectively—they need to differentiate their database models somehow.
This use case can be easily solved by the solution provided by the spark but assuming such similarity is not limited to one table, assuming this similarity approaches countable infinity, in such case, manually redefining names of tables in model files will not only be tedious but inefficient.

My proposal remains as above: a means to use prefixes in table names. CodeIgniter already provides a $config[database][dbprefix] = 'pfx'—the "pfx" is an assumption—option which can be used like so

`$this->db->prefix("table_name"); // should result in pfx_table_name

However, the PHPActiveRecord spark does not respect this, since…

class Test extends ActiveRecord\Model
{
  static $table_name;

  function __construct()
  {
    parent::__construct();
    $table_name = $this->db->prefix('tests');

    // OR…
    // $CI =& get_instance();
    // $CI->load->database();
    // $table_name = $CI->db->prefix('tests');
  }
}

… still assumes the table name is tests by virtue of…

[04-Sep-2013 22:15:29 Europe/Berlin] PHP Fatal error:  Uncaught exception 'ActiveRecord\DatabaseException' with message '42S02, 1146, Table 'database.tests' doesn't exist' in /Applications/MAMP/htdocs/app_name/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Connection.php:313
Stack trace:
#0 /Applications/MAMP/htdocs/app_name/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/adapters/MysqlAdapter.php(25): ActiveRecord\Connection->query('SHOW COLUMNS FR...')
#1 /Applications/MAMP/htdocs/app_name/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Connection.php(254): ActiveRecord\MysqlAdapter->query_column_info('`tests`')
#2 /Applications/MAMP/htdocs/app_name/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Table.php(370): ActiveRecord\Connection->columns('`tests`')
#3 /Applications/MAMP/htdocs/app_name/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Cache.php(67): ActiveRecord\Table->ActiveRecord\{closure}()
#4 /Applications/MAMP/htdocs/trc_ci/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Table.php(370): Act in /Applications/MAMP/htdocs/trc_ci/sparks/php-activerecord/0.0.2/vendor/php-activerecord/lib/Connection.php on line 313

…but in my MySQL database, there is a "pfx_tests".

@machuga
Copy link
Owner

machuga commented Sep 5, 2013

Hi,

I don't really add features to this package anymore, but I think someone just made a PR for DB prefixes which is slightly different.

It could be possible, but I'd recommend doing something like what you've done but tweaked slightly to use proper syntax. Then just extend from that base class.

class BaseModel extends ActiveRecord\Model
{
    /**
     * Retrieves the name of the table for this Model.
     *
     * @return string
     */
    public static function table_name()
    {
        return get_instance()->db->prefix(static::table()->table);
    }
}

This should be close to the right syntax. You can further example the Model class inside of PHPActiveRecord to verify.

Best of luck!
Matt

@igbanam igbanam closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants