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 crashes when table has invalid FKs referencing non-existent tables #185

Closed
yii-bot opened this issue Aug 28, 2016 · 5 comments
Closed
Labels
Milestone

Comments

@yii-bot
Copy link

yii-bot commented Aug 28, 2016

This issue has originally been reported by @tekord at yiisoft/yii2#12358.
Moved here by @SilverFire.


What steps will reproduce the problem?

I have MySQL table:

CREATE TABLE `support_issue` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `type` VARCHAR(64) NULL DEFAULT NULL,
    `issued_by` INT(10) UNSIGNED NULL DEFAULT NULL,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `subject` VARCHAR(128) NOT NULL,
    `message` VARCHAR(2048) NOT NULL,
    `resolved_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `type` (`type`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;

Then I try to generate model class through Gii generator:

Table Name
support_issue

Model Class 
SupportIssue

Namespace
common\models

Base Class
yii\db\ActiveRecord

Database Connection ID
db

[_] Use Table Prefix
Generate Relations
All relations

[_]  Generate Labels from DB Comments
[_]  Generate ActiveQuery
[CHECKED]  Enable I18N

Message Category
app
[_] Use Schema Name
Code Template
default (G:\xampp\htdocs\business-buddy\vendor\yiisoft\yii2-gii\generators\model/default)

What is the expected result?

Successfully generated model.

What do you get instead?

PHP Notice – yii\base\ErrorException

Trying to get property of non-object
1. in G:\xampp\htdocs\business-buddy\vendor\yiisoft\yii2-gii\generators\model\Generator.php at line 640

That happened just after 'Preview' button click.

Here is the screenshot: https://i.gyazo.com/5eff52de9661bcf7659fe1eada2a0f1d.png

Additional info

Q A
Yii version 2.0.9
PHP version 7.0.8
Operating system Windows 8.1

UPD: I just noticed this happens on any table. Tried to clean vendor folder and run composer update, but no effect.

@tekord
Copy link

tekord commented Sep 4, 2016

I noticed that the error occurs if the option 'All relations' or 'All relations with Inverse' are selected. Maybe something wrong with my tables. Will look at it.

@tekord
Copy link

tekord commented Sep 4, 2016

Yes, I have a table which has broken foreign keys (links to non-existing tables). I guess we need some checks in Gii model generator to handle this case.

@samdark
Copy link
Member

samdark commented Sep 4, 2016

Any idea on how to check it?

@samdark samdark added type:enhancement Enhancement and removed to be verified labels Sep 4, 2016
@samdark samdark changed the title Strange Gii crash Gii crashes when table has invalid FKs Sep 4, 2016
@samdark samdark changed the title Gii crashes when table has invalid FKs Gii crashes when table has invalid FKs referencing non-existent tables Sep 4, 2016
@tekord
Copy link

tekord commented Sep 4, 2016

I found similar issue #34, it is closed, but my issue is still relevent. It has difference.

To reproduce execute the following SQL:

SET FOREIGN_KEY_CHECKS = 0;
CREATE TABLE `tour_to_image_links` (
    `product_id` INT(10) UNSIGNED NOT NULL,
    `image_id` INT(10) UNSIGNED NOT NULL,
    `position` SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0',
    `type` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
    PRIMARY KEY (`product_id`, `image_id`),
    INDEX `FK_tour_to_image_links_image` (`image_id`),
    CONSTRAINT `FK_tour_to_image_links_image` FOREIGN KEY (`image_id`) REFERENCES `images` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
    CONSTRAINT `FK_tour_to_image_links_product` FOREIGN KEY (`product_id`) REFERENCES `tours` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;

CREATE TABLE `images` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `file` VARCHAR(1024) NOT NULL,
    `url` VARCHAR(1024) NOT NULL,
    `title` VARCHAR(255) NULL DEFAULT NULL,
    `description` TEXT NULL,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `status` TINYINT(3) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPACT
AUTO_INCREMENT=22
;

SET FOREIGN_KEY_CHECKS = 1;

Then try to generate model for ANY table, not only of that new tables. Error will be thrown.

Next - drop the valid FK:

ALTER TABLE `tour_to_image_links`
    DROP FOREIGN KEY `FK_tour_to_image_links_image`;

Then NO ERROR thrown on model generation, although the table still has broken FK. This case is handled by that issue (#34).

Still don't know why this happening. Gonna check later, maybe give a solution.

@adipriyantobpn
Copy link
Contributor

Same with me.

I just tried to do same steps done by @samdark at commit aeec43d in addInverseRelations(), then no error thrown again.

@samdark samdark closed this as completed in 20046a8 Mar 8, 2018
@samdark samdark added this to the 2.0.7 milestone Mar 8, 2018
@samdark samdark added type:bug Bug and removed type:enhancement Enhancement labels Mar 8, 2018
lav45 added a commit to lav45/yii2-gii that referenced this issue Oct 24, 2018
* upstream/master:
  Remove useless import of `Yii` from CRUD generator search model template
  Fixes yiisoft#379: Fixed bug in view page where delete button not work well
  Fixes yiisoft#366: Option to allow standardized class names capitals in model generator
  Fixes yiisoft#327: Fixed bug in Model generator when $baseClass is an abstract class
  Fixes yiisoft#366: Better class and file names for uppercase tables
  Update composer.json (yiisoft#364)
  Fix codestyle (yiisoft#362)
  prepare for next release
  release version 2.0.7
  Register CSRF meta-tags dynamically
  Updated CHANGELOG [ci skip]
  Fixed rules generation for JSON columns
  Removed redundant line from license [skip ci]
  added database version to issue template (yiisoft#352) [skip ci]
  docs/guide-ja revised [ci skip] (yiisoft#349)
  Updated issue template [skip ci]
  Fixes yiisoft#185: Fix bug in Model generators when FKs pointing to non-existing tables
  Fixes yiisoft#340: Fix bug in CRUD SearchModel generator
  Require Yii 2.0.14 (yiisoft#339)

Conflicts:
	composer.json
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

5 participants