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 can not generate model from mysql tables #407

Open
mxmorozov opened this issue Apr 14, 2019 · 6 comments
Open

Gii can not generate model from mysql tables #407

mxmorozov opened this issue Apr 14, 2019 · 6 comments
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug

Comments

@mxmorozov
Copy link

When I try to use Model Generator
PHP Warning – yii\base\ErrorException

  1. in .../vendor/yiisoft/yii2-gii/src/generators/model/Generator.php at line 523
    array_flip(): Can only flip STRING and INTEGER values!

mysql 8
php 7.3
Yii 2.0.17
centos 7

@alex-code
Copy link
Contributor

Can you show your table schema including relations as the error is in the section that builds relations.

@samdark samdark transferred this issue from yiisoft/yii2 Apr 15, 2019
@samdark samdark added status:to be verified Needs to be reproduced and validated. type:bug Bug status:need more info labels Apr 15, 2019
@yii-bot
Copy link

yii-bot commented Apr 15, 2019

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

This is an automated comment, triggered by adding the label status:need more info.

@mxmorozov
Copy link
Author

mxmorozov commented Apr 15, 2019

It seems any mysql foreign key will be sufficient to get error.
For example

CREATE TABLE `a` (
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE `b` (
  `id` int(11) NOT NULL,
  `a_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `a_id` (`a_id`),
  CONSTRAINT `b_ibfk_1` FOREIGN KEY (`a_id`) REFERENCES `a` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8"

Error on generating models for both tables

@mxmorozov
Copy link
Author

I tested further and model generator fails even without foreign keys. Even with "Generate Relations from Current Schema" disabled. May be PHP 7.3 is the cause?

@samdark
Copy link
Member

samdark commented Apr 15, 2019

Can you downgrade and check it?

@mxmorozov
Copy link
Author

mxmorozov commented Apr 15, 2019

I found mysql scheme pattern that causes error

CREATE TABLE `a` (
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
)

CREATE TABLE `b` (
  `id` int(11) NOT NULL,
  `a_id` int(11) NOT NULL,
  `some_field` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `a_id` (`a_id`, `some_field`),
  CONSTRAINT `a_id` FOREIGN KEY (`a_id`) REFERENCES `a` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
)

When you have a table like 'b' in your db, model generator will fail on every table because it scans all tables relations.
Also if rename unique key or foreign key - error will gone. Seems identical names (a_id) are a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug
Projects
None yet
Development

No branches or pull requests

4 participants