Gii Model Generator doesn't use non-PK FKs in Relation Tables #2169

sanchopancho13 opened this Issue Mar 5, 2013 · 3 comments


3 participants

The default Gii Model Generator (ModelCode.php) has a method called generateRelations. This method loops over all database tables, looking at all the foreign keys and creating a 'relations' array. This array is used to construct the relations method in the generated model class.

This code fails to look at any foreign key in a MANY_MANY table that is not also a primary key. These columns represent metadata about relationships. For example:

Table CARS = car_id (PK), car_model, car_manufacturer
Table OWNERS = owner_id (PK), owner_name
Table DEALERS = dealer_id (PK), dealer_name, dealer_address
Table PURCHASES = owner_id (PK/FK), car_id (PK/FK), dealer_id (FK), purchase_date

In this example, the PURCHASES table has additional metadata about the purchase. When generating the model for DEALERS, Gii will ignore the PURCHASES.dealer_id relationship, and will not include a "purchases" relationship inside the Dealer class.

Is it ok if I push a change for this issue? I tried following the instructions on the "Git workflow for Yii contributors" page, but I think I lack the permissions to perform the push. Or maybe I did something else wrong. The error message I get is "Repository not found."


AnatolyRugalev commented Mar 5, 2013

Plaase follow this instruction.

  1. Create your own fork of yiisoft/yii repositiry (there is button at top right).
  2. Push changes to new branch in your repo (sanchopancho13/yii)
  3. Create pull request on your repository page

You can attach PR to this issue by adding #2169 into title or comment text


samdark commented Nov 9, 2016

Won't be implemented since 1.1 is reaching EOL and new features aren't accepted.

samdark closed this Nov 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment