Skip to content

gii/model --tableName=prefix_* generates classnames from the full table name instead of from the matching characters as indicated by --help #435

@lennartvdd

Description

@lennartvdd

What steps will reproduce the problem?

  1. Make sure the default db Connection does not have tablePrefix set.
  2. Create two or more tables with some prefix, for example: prefix_some_model, prefix_another_model
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions