What steps will reproduce the problem?
- Make sure the default
db Connection does not have tablePrefix set.
- Create two or more tables with some prefix, for example:
prefix_some_model, prefix_another_model
- run the command
./yii gii/model --tableName=prefix_*
What's expected?
Two new classes, named: SomeModel and AnotherModel
What do you get instead?
Two new classes, named PrefixSomeModel and PrefixAnotherModel
Additional info
| Q |
A |
| Yii version |
2.0.34 |
| Yii Gii version |
2.1.4 |
| PHP version |
7.3.14 |
| Database version |
MariaDB v15.1 |
| Operating system |
Debian 10.3 |
yii\gii\generators\model\Generator::generateClassName() generates classnames by matching the following patterns:
$patterns[] = "/^{$db->tablePrefix}(.*?)$/";
$patterns[] = "/^(.*?){$db->tablePrefix}$/";
if (strpos($this->tableName, '*') !== false) {
$pattern = $this->tableName;
if (($pos = strrpos($pattern, '.')) !== false) {
$pattern = substr($pattern, $pos + 1);
}
$patterns[] = '/^' . str_replace('*', '(\w+)', $pattern) . '$/';
}
When $db->tablePrefix is empty, this results in an array containing three patterns of which the first two would both be /^(.*?)$/ and the third /^prefix_(\w+)$/. Since patterns are matched in the order in which they appear in the array, the prefix pattern would never match.
Solution would be to check if $db->tablePrefix is empty and not set or change the order of the patterns.