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".