Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lennartvdd opened this issue Jun 2, 2020 · 2 comments
Labels

Comments

@lennartvdd
Copy link

lennartvdd commented Jun 2, 2020

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.

@samdark samdark added the type:bug Bug label Jun 10, 2020
@samdark
Copy link
Member

samdark commented Jun 10, 2020

Sounds like clearly a bug. Do you have time for a pull request fixing it?

ditibal added a commit to ditibal/yii2-gii that referenced this issue Aug 5, 2021
@WinterSilence
Copy link
Contributor

@samdark i'm think need fix hint or add optional key for switch mode because you cant select more than one model without cutting prefix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants