yii2-gii
yii2-gii copied to clipboard
Gii can not generate model from mysql tables
When I try to use Model Generator PHP Warning – yii\base\ErrorException
- 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
Can you show your table schema including relations as the error is in the section that builds relations.
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.
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
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?
Can you downgrade and check it?
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.