Skip to content

Commit

Permalink
createActiveRecordModel(): support prefixing table name with database…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
winternet-studio committed Apr 22, 2022
1 parent 9e79895 commit a74e16a
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ModelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,16 @@ public static function filterAttributes($all_attributes, $attributes_to_keep) {
* Dynamically create an ActiveRecord model
*
* @param array $params : Array with these entries:
* - `tableName` (req.) : Name of database table
* - `tableName` (req.) : Name of database table. Database can be specified using normal SQL notation: `databasename.tablename`
*
* @return string : Class name to be instantiated
*/
public static function createActiveRecordModel($params) {
if (preg_match("/[^a-z0-9_]/i", $params['tableName'])) {
if (preg_match("/[^a-z0-9_\\.]/i", $params['tableName'])) {
new \winternet\yii2\UserException('Table name for creating ActiveRecord class has invalid characters.', ['TableName' => $params['tableName']]);
}

$className = $params['tableName'];
$className = str_replace('.', '__', $params['tableName']);

$phpCode = "class ". $className ." extends \yii\db\ActiveRecord { public static function tableName() {return '". $params['tableName'] ."';} }". PHP_EOL;
eval($phpCode);
Expand Down

0 comments on commit a74e16a

Please sign in to comment.