yiisoft / yii

Yii PHP Framework 1.1.x
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
4.85k stars 2.28k forks source link

Gii is missing model relations (PostgreSQL) #2744

Closed kjusupov closed 11 years ago

kjusupov commented 11 years ago
  1. Gii / Model Generator is not able to detect model relation client.country_id
  2. But, it's able to detect for ref_bank.country_id and ref_region.country_id
  3. OS: Ubuntu 12.04 LTS Precise Pangolin (64-bit)
  4. Apache version: 2.2.22
  5. PHP version: 5.3.10
  6. PostgreSQL version: 9.1.9
  7. Yii version: 1.1.13

Preview models/Country.php shows as below (I expected @property Client[] $clients" to be included as well.

But, if I name the table as "ref_client" (with the same structure), than it works just fine...

Is this how Gii was supposed to work?

 * The followings are the available model relations:
 * @property Bank[] $banks
 * @property Region[] $regions
 */
     /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'banks' => array(self::HAS_MANY, 'Bank', 'country_id'),
            'regions' => array(self::HAS_MANY, 'Region', 'country_id'),
        );
    }

My tables:

dev_test=# \dt
               List of relations
 Schema |    Name     | Type  |     Owner      
--------+-------------+-------+----------------
 public | client      | table | tioli_app_user
 public | ref_bank    | table | tioli_app_user
 public | ref_country | table | tioli_app_user
 public | ref_region  | table | tioli_app_user
(4 rows)

TABLE: ref_country

dev_test=# \d ref_country 
                                  Table "public.ref_country"
 Column |           Type           |                        Modifiers                         
--------+--------------------------+----------------------------------------------------------
 id     | integer                  | not null default nextval('ref_country_id_seq'::regclass)
 code   | character varying        | not null
 name   | character varying        | not null
 status | boolean                  | default true
 cuid   | integer                  | default 1
 ctime  | timestamp with time zone | not null default now()
 muid   | integer                  | 
 mtime  | timestamp with time zone | 
Indexes:
    "ref_country_pkey" PRIMARY KEY, btree (id)
    "ref_country_code_key" UNIQUE CONSTRAINT, btree (code)
    "ref_country_name_key" UNIQUE CONSTRAINT, btree (name)
    "ref_country_status_idx" btree (status)
Referenced by:
    TABLE "client" CONSTRAINT "client_country_id_fkey" FOREIGN KEY (country_id) REFERENCES ref_country(id)
    TABLE "ref_bank" CONSTRAINT "ref_bank_country_id_fkey" FOREIGN KEY (country_id) REFERENCES ref_country(id)
    TABLE "ref_region" CONSTRAINT "ref_region_country_id_fkey" FOREIGN KEY (country_id) REFERENCES ref_country(id)

TABLE: ref_region

dev_test=# \d ref_region
                                    Table "public.ref_region"
   Column   |           Type           |                        Modifiers                        
------------+--------------------------+---------------------------------------------------------
 id         | integer                  | not null default nextval('ref_region_id_seq'::regclass)
 country_id | integer                  | default 1
 code       | character varying        | not null
 name       | character varying        | not null
 status     | boolean                  | default true
 cuid       | integer                  | default 1
 ctime      | timestamp with time zone | not null default now()
 muid       | integer                  | 
 mtime      | timestamp with time zone | 
Indexes:
    "ref_region_pkey" PRIMARY KEY, btree (id)
    "ref_region_code_key" UNIQUE CONSTRAINT, btree (code)
    "ref_region_name_key" UNIQUE CONSTRAINT, btree (name)
    "ref_region_country_id_idx" btree (country_id)
    "ref_region_status_idx" btree (status)
Foreign-key constraints:
    "ref_region_country_id_fkey" FOREIGN KEY (country_id) REFERENCES ref_country(id)
Referenced by:
    TABLE "ref_state" CONSTRAINT "ref_state_region_id_fkey" FOREIGN KEY (region_id) REFERENCES ref_region(id)

TABLE: ref_bank

dev_test=# \d ref_bank
                                      Table "public.ref_bank"
     Column     |           Type           |                       Modifiers                       
----------------+--------------------------+-------------------------------------------------------
 id             | integer                  | not null default nextval('ref_bank_id_seq'::regclass)
 code           | character varying        | not null
 name           | character varying        | not null
 account_length | integer                  | not null
 country_id     | integer                  | 
 status         | boolean                  | default true
 cuid           | integer                  | default 1
 ctime          | timestamp with time zone | not null default now()
 muid           | integer                  | 
 mtime          | timestamp with time zone | 
Indexes:
    "ref_bank_pkey" PRIMARY KEY, btree (id)
    "ref_bank_name_key" UNIQUE CONSTRAINT, btree (name)
    "ref_bank_country_id_idx" btree (country_id)
    "ref_bank_status_idx" btree (status)
Check constraints:
    "ref_bank_account_length_check" CHECK (account_length > 0)
Foreign-key constraints:
    "ref_bank_country_id_fkey" FOREIGN KEY (country_id) REFERENCES ref_country(id)

TABLE: client

                                         Table "public.client"
        Column         |           Type           |                      Modifiers                      
-----------------------+--------------------------+-----------------------------------------------------
 id                    | integer                  | not null default nextval('client_id_seq'::regclass)
 code                  | character varying        | not null
 name                  | character varying        | not null
 address1              | character varying(100)   | not null
 address2              | character varying(100)   | 
 address3              | character varying(100)   | 
 city                  | character varying(100)   | 
 postcode              | character varying(10)    | 
 state_id              | integer                  | 
 country_id            | integer                  | 
 phone                 | character varying(15)    | 
 fax                   | character varying(15)    | 
 email                 | character varying(100)   | 
 commence_date         | date                     | not null
 termination_date      | date                     | 
 termination_reason    | character varying        | 
 client_contact_person | character varying        | 
 status                | boolean                  | default true
 remarks               | character varying        | 
 cuid                  | integer                  | default 1
 ctime                 | timestamp with time zone | default now()
 muid                  | integer                  | 
 mtime                 | timestamp with time zone | 
Indexes:
    "client_pkey" PRIMARY KEY, btree (id)
    "client_code_key" UNIQUE CONSTRAINT, btree (code)
    "client_name_key" UNIQUE CONSTRAINT, btree (name)
    "client_country_id_idx" btree (country_id)
    "client_status_idx" btree (status)
Foreign-key constraints:
    "client_country_id_fkey" FOREIGN KEY (country_id) REFERENCES ref_country(id)
    "client_state_id_fkey" FOREIGN KEY (state_id) REFERENCES ref_state(id)
samdark commented 11 years ago

@kjusupov would you please check if code from master is any better?

kjusupov commented 11 years ago

@samdark nope, same behaviour... The moment I put "ref_" prefix it starts working, otherwise it cannot detect the relations

cebe commented 11 years ago

But, if I name the table as "ref_client" (with the same structure), than it works just fine...

This is because when you give prefix in gii it will only check tables that have the prefix. prefix is ment to be used if you have multiple tables in one db that are not related. If all your tables are relevant, just do not enter prefix in gii.