yii2-gii icon indicating copy to clipboard operation
yii2-gii copied to clipboard

Gii can not generate model from mysql tables

Open mxmorozov opened this issue 6 years ago • 6 comments

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

mxmorozov avatar Apr 14 '19 16:04 mxmorozov

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

alex-code avatar Apr 15 '19 08:04 alex-code

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.

yii-bot avatar Apr 15 '19 10:04 yii-bot

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 avatar Apr 15 '19 15:04 mxmorozov

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?

mxmorozov avatar Apr 15 '19 15:04 mxmorozov

Can you downgrade and check it?

samdark avatar Apr 15 '19 16:04 samdark

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.

mxmorozov avatar Apr 15 '19 18:04 mxmorozov