"Key column 'username' doesn't exist in table" - php

I am creating three tables and when trying to tie the primary and foreign keys I am receiving the error message "Key column 'username' doesn't exist in table".
Could someone take a look at my code and tell me what I am doing wrong? I have tried dropping the database and revamped the tables a few times but I am still getting the same message. Here is my code, thank you in advance for any help!
create database testproject
use testproject
create table caller_info
(
caller_id int(11) unsigned auto_increment primary key not null,
first_name varchar(35) not null,
Last_name varchar(35) not null,
phone_number int(25) not null
);
create table caller_call_record
(
call_record_id int(11),
Call_Description varchar(50),
franchise_id int(10) not null,
email varchar(40) not null,
username varchar(25) primary key not null
);
create table caller_escalation
(
call_escalation_id int(11) unsigned auto_increment not null,
Second_Level varchar(5) not null,
caller_id int(11) not null,
PRIMARY KEY(call_escalation_id),
FOREIGN KEY(caller_id)
REFERENCES caller_info(caller_id),
FOREIGN KEY(username) REFERENCES caller_call_record(username)
);

As pointed out, your table caller_escalation needs a column called username in order to create the foreign key.
It sounds like once you've added that, you're now getting a Cannot add foreign key constraint error from MySQL. The first thing that comes to mind with this kind of error is that you're using the wrong engine for your tables. You are most likely using MyISAM which does not support foreign key references - in order to use these, you will need to change the engine on all of your tables to InnoDB.

try this:
create table caller_call_record
(
call_record_id int(11),
Call_Description varchar(50),
franchise_id int(10) not null,
email varchar(40) not null,
username varchar(25) not null,
PRIMARY KEY (username)
);
create table caller_escalation
(
call_escalation_id int(11) unsigned auto_increment not null,
Second_Level varchar(5) not null,
caller_id int(11) not null,
username varchar(25) not null,
PRIMARY KEY(call_escalation_id,username),
FOREIGN KEY(caller_id)
REFERENCES caller_info(caller_id),
FOREIGN KEY(username) REFERENCES caller_call_record
);

The table caller_escalation also needs a column username, which is not there
create table caller_escalation
(
call_escalation_id int(11) unsigned auto_increment not null,
Second_Level varchar(5) not null,
caller_id int(11) unsigned not null, ;; <--- added unsigned type here
PRIMARY KEY(call_escalation_id),
username varchar(25) not null, ;; <--- added field here
FOREIGN KEY(caller_id)
REFERENCES caller_info(caller_id),
FOREIGN KEY (username) REFERENCES caller_call_record (username)
);
Also ensure the column data types are the same in both tables. You have "caller_id int(11) unsigned" in caller_info and "caller_id int(11)" in caller_escalation. I added the unsigned specifier above to make it work.

Related

MySQL foreign key issue

I am having a problem trying to load my database into MySQL as I am having a few errors with my foreign keys. Things I have tried to do to fix this issue is:
- Putting foreign keys after the primary keys
Eg:
CREATE TABLE IF NOT EXISTS Passenger (
.......
PRIMARY KEY(tNum),
FOREIGN KEY (fNum) REFERENCES Flights(fNum),
FOREIGN KEY (fDate) REFERENCES Flights(fDate),
FOREIGN KEY (sCity) REFERENCES Flights(sCity),
FOREIGN KEY (eCity) REFERENCES Flights(eCity)
);
- Bunching the foreign keys in the format of
Eg:
ALTER TABLE Passenger ADD FOREIGN KEY (fNum, fDate, sCity, eCity) REFERENCES Flights(fNum, fDate, sCity, eCity);
The error I get is:
1005 - Can't create table 'airline.#sql-1d7_7c' (errno: 150)
My full code is:
DROP DATABASE IF EXISTS airline;
CREATE DATABASE IF NOT EXISTS airline;
USE airline;
CREATE TABLE IF NOT EXISTS Flights (
fNum char(6) not null,
pID char(4) not null,
fDate DATE not null,
eDate DATE not null,
sTime char(4) not null,
lTime char(4) not null,
sOStart char(4) null,
sOEnd char(4) null,
sCity varchar(30) not null,
eCity varchar(30) not null,
sOCity varchar(30),
sNum char(5) not null,
PRIMARY KEY (fNum)
);
CREATE TABLE IF NOT EXISTS Passenger (
tNum char(4) not null,
dPurch DATE not null,
pMethod varchar(30) not null,
fNum char(6) not null,
fDate DATE not null,
sCity varchar(30) not null,
eCity varchar(30) not null,
tType varchar(30) not null,
Price decimal(4,2) not null,
iType varchar(30) not null,
idNum char(8) not null,
fName varchar(30) not null,
lName varchar(30) not null,
Sex char(1) not null,
pAddress varchar(30) not null,
pPhone char(8) not null,
pEmail varchar(30) not null,
PRIMARY KEY(tNum)
);
CREATE TABLE IF NOT EXISTS Planes (
pID char(4) not null,
pType char(3) not null,
pDesc varchar(30) not null,
pRange char(4) not null,
Capacity char(3) not null,
mDate DATE not null,
pDate DATE not null,
sDate DATE not null,
PRIMARY KEY (pID)
);
CREATE TABLE IF NOT EXISTS Staff (
sNum char(5) not null,
sName varchar(30) not null,
sDOB DATE not null,
sAddress varchar(30) not null,
pCompany varchar(30) ,
pStart DATE ,
pEnd DATE ,
jID char(1) not null,
PRIMARY KEY (sNum)
);
CREATE TABLE IF NOT EXISTS Emergency (
eID char(5) not null,
sNum char(5) not null,
eName varchar(30) not null,
eAddress varchar(30) not null,
ePhone char(8) not null,
eEmail varchar(30) not null,
eRelationship varchar(30) not null,
PRIMARY KEY(eID)
);
CREATE TABLE IF NOT EXISTS Pilot (
sNum char(5) not null,
pID char(4) not null,
cDate DATE not null,
jID char(1) not null,
PRIMARY KEY(jID)
);
CREATE TABLE IF NOT EXISTS Attendant (
sNum char(5) not null,
tSDate Date not null,
tFDate Date not null,
tDesc Varchar(30) not null,
jID Char(1) not null,
PRIMARY KEY(jID)
);
ALTER TABLE Flights ADD FOREIGN KEY (pID) REFERENCES Planes(pID);
ALTER TABLE Flights ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Passenger ADD FOREIGN KEY (fNum) REFERENCES Flights(fNum);
ALTER TABLE Passenger ADD FOREIGN KEY (fDate) REFERENCES Flights(fDate);
ALTER TABLE Passenger ADD FOREIGN KEY (sCity) REFERENCES Flights(sCity);
ALTER TABLE Passenger ADD FOREIGN KEY (eCity) REFERENCES Flights(eCity);
ALTER TABLE Emergency ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Pilot ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Pilot ADD FOREIGN KEY (pID) REFERENCES Planes(pID);
ALTER TABLE Pilot ADD FOREIGN KEY (jID) REFERENCES Staff(jID);
ALTER TABLE Attendant ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Attendant ADD FOREIGN KEY (jID) REFERENCES Staff(jID);
For me this is the one it fails on first
ALTER TABLE Passenger ADD FOREIGN KEY (fDate) REFERENCES Flights(fDate);
Try adding an index on Flights.fdate first, then doing the foreign key and doing that for additional references until it works. Let me know if that works for you, it did for me.
By the way you might want to reconsider your schema there. Joins and relations should really be done on integers only. That schema will bog down fast as it grows. Also, and maybe this is just personal preference but I prefer first_name to fName. Easier to read for other developers, this isn't 1986, we can have human readbable names now :-)

MySQL Foreign Key Constraint fails on Linux Server but works on Windows XAMPP

I have an issue that I can't figure out for the life of me. I have been creating an application in PHP/MySQL on windows using XAMPP but now I am trying to test it on a Linux based LAMP server and I get the following error when trying to create a table with foreign key constraints:
"3 - photoProjectItem table creation: Cannot add foreign key constraint"
When I run the code on XAMPP in Windows it works fine, but not within Linux. These are the tables I am trying to create:
CREATE TABLE IF NOT EXISTS generalSecurity(
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(100) NOT NULL,
firstname VARCHAR(100) NOT NULL,
secondname VARCHAR(100) NOT NULL,
accessLevel ENUM('admin', 'contributer', 'subscriber') NOT NULL,
Email VARCHAR (200) UNIQUE NOT NULL
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS generalSiteInfo (
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
siteName VARCHAR(100) NOT NULL,
siteOwnerID INT(10) UNSIGNED NOT NULL,
INDEX par_id4(siteOwnerID),
FOREIGN KEY(siteOwnerID)
REFERENCES generalSecurity(id)
ON DELETE CASCADE,
siteEmail VARCHAR(100) NOT NULL,
siteAbout VARCHAR(9999) NOT NULL,
currentTheme VARCHAR(1000) NOT NULL,
siteCreateDate TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS Project (
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
projectAuthorID INT(10) UNSIGNED NOT NULL,
INDEX par_id(projectAuthorID),
FOREIGN KEY(projectAuthorID)
REFERENCES generalSecurity(id)
ON DELETE CASCADE,
projectName VARCHAR(100) NOT NULL,
projectBlurb VARCHAR(5000) NULL,
projectTheme VARCHAR(100) NOT NULL,
projectDate TIMESTAMP
) ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS ProjectItem (
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
authorID INT(10) UNSIGNED NOT NULL,
INDEX par_id2(authorID),
FOREIGN KEY(authorID)
REFERENCES generalSecurity(id)
ON DELETE CASCADE,
projectID INT(10) UNSIGNED NOT NULL,
INDEX par_id3(projectID),
FOREIGN KEY(projectID)
REFERENCES project(id)
ON DELETE CASCADE,
itemType ENUM('image', 'text') NOT NULL,
photoFileName VARCHAR(100) NULL,
itemName VARCHAR(1000) NOT NULL,
entry VARCHAR(5000) NULL,
photoMeta VARCHAR(5000) NOT NULL,
date TIMESTAMP,
deleted BOOLEAN NOT NULL
) ENGINE=INNODB;
it fails when trying to create table "ProjectItem". Can you guys see anything I am missing?
I really appreciate any help that can be given
---edit---
it works when I remove the lines
INDEX par_id3(projectID),
FOREIGN KEY(projectID)
REFERENCES project(id)
ON DELETE CASCADE,
but I can't see any obvious issues with the relationship
The issue was that apparantly MYSQL in Linux is case sensitive whereas in Windows it doesn't appear to be. I was trying to create a relationship for the table "project" when it is called "Project"

Yii2-Integrity constraint violation – yii\db\IntegrityException

Integrity constraint violation – yii\db\IntegrityException
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`jts`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`))
The SQL being executed was: INSERT INTO `employee` (`client_code`, `company_name`, `emp_email`, `emp_mobile`, `emp_first_name`, `emp_last_name`) VALUES ('12345678', 'PVPPCOE', 'saurabhkulkarni2010#hotmail.com', '+449029792183', 'Saurabh', 'Kulkarni')
Error Info: Array
(
[0] => 23000
[1] => 1452
[2] => Cannot add or update a child row: a foreign key constraint fails (`jts`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`))
)
I have three tables Client, Employee and create_client, out of which client and employee has two foreign keys.
This problem is showing when I try to insert data from create_client to employee which has exact same field.
What should I do so that I can insert two tables at one i.e create_client and employee
UPDATE-
Table Structure
1)client
CREATE TABLE IF NOT EXISTS `client` (
`id` int(11) NOT NULL,
`client_code` varchar(11) NOT NULL,
`company_name` varchar(45) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
2)create_client
CREATE TABLE IF NOT EXISTS `create_client` (
`id` int(11) NOT NULL,
`client_code` varchar(11) NOT NULL,
`company_name` varchar(45) NOT NULL,
`emp_email` varchar(45) NOT NULL,
`emp_mobile` varchar(45) NOT NULL,
`emp_first_name` varchar(45) NOT NULL,
`emp_last_name` varchar(45) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8;
3)employee
CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL,
`client_code` varchar(11) NOT NULL,
`company_name` varchar(45) NOT NULL,
`emp_email` varchar(45) NOT NULL,
`emp_mobile` varchar(45) NOT NULL,
`emp_first_name` varchar(45) NOT NULL,
`emp_last_name` varchar(45) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
This is my table structure, first user will create client that means first it will update create_client. Now if user wants he can add many employee under one client code now user will update employee table from Yii2 dynamic form widget.
For using this widget I have created one table call client this will store only client_code and company name remaining data will go in employee table e.g emp_mobile,emp_email, emp_first_name, emp_last_name.
Now this problem is pop up when user first enters data into create_client table.
everything is working between client and employee table user able to enter as many employee as it wants using Yii2 dynamic form Widget but not working for create_client
Apparently you also have a foreign key in the employee table referencing the client_code of the client table, so you can only use client_code values that already exist in the client table.
I don't know what the structure of the create_client table looks like and what its purpose is, but based on your info I assume that you should first insert the data in client, then in employee and finally in create_client, so that every foreign key's value exists in its respective table.
If this is not correct, please post your table structures and the data or queries that could be causing this.
Edit after your comment: I assume that your table structure looks like this:
CREATE TABLE `create_client` (
`client_code` INT UNSIGNED NOT NULL,
`company_name` VARCHAR(255) NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
UNIQUE KEY `client_code` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `client` (
`client_code` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(255) NOT NULL,
PRIMARY KEY `client_code` (`client_code`),
CONSTRAINT `client_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `create_client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `employee` (
`client_code` INT UNSIGNED NOT NULL,
`company_name` VARCHAR(255) NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
KEY `client_code` (`client_code`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
However, I would suggest to use client as the parent table and move the foreign key constraint to create_client, so that both employee and create_client have a foreign key of client. I would also like to normalise this and get rid of company_name in the child tables:
CREATE TABLE `client` (
`client_code` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(255) NOT NULL,
PRIMARY KEY `client_code` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `create_client` (
`client_code` INT UNSIGNED NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
UNIQUE KEY `client_code` (`client_code`),
CONSTRAINT `create_client_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `employee` (
`client_code` INT UNSIGNED NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
KEY `client_code` (`client_code`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
The next improvement would be to use the employee table for both unique and non-unique client_code inserts. Instead of using 2 identical tables, apart from the unique key, you could do the unique validation in Yii instead of in MySQL. You can specify the table name in the tableName() method of the CreateClient model . This will remain:
CREATE TABLE `client` (
`client_code` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`company_name` VARCHAR(255) NOT NULL,
PRIMARY KEY `client_code` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE `employee` (
`client_code` INT UNSIGNED NOT NULL,
`emp_mobile` VARCHAR(255) NOT NULL,
`emp_email` VARCHAR(255) NOT NULL,
`emp_first_name` VARCHAR(255) NOT NULL,
`emp_last_name` VARCHAR(255) NOT NULL,
KEY `client_code` (`client_code`),
CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`client_code`) REFERENCES `client` (`client_code`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
This should allow you to use the dynamic form, with Client as the main model and Employee as the items model.
On Yii2 using IO Generator (Model) and MySQL I have found the following.
I have seen SET '' instead of SET NULL in update SQL statement(s) on field(s) with foreign key constraints that have caused a "Integrity constraint violation".
My "fix" is to add a default rule on the foreign key field in the model, like so:
public function rules()
{
return [
//
// your other rules
//
[['field_name_with_foreign_key'], 'default', 'value' => null],
];
}
Hope this helps someone.
remove all foreign key checks and simplify database.
the problem lies in your foreign key structure.
learn carefully about primary keys and foreign keys.
make new database again without foreign keys then check.

#1005 - Can't create table 'database.viewers' (errno: -1)

this table is already work fine
create table posts (
id bigint(20) unsigned not null auto_increment,
title varchar(200) not null,
content text,
mdesc varchar(340),
pdate timestamp not null default current_timestamp,
lupdate timestamp not null default '0000-00-00 00:00:00',
perma varchar(120) not null,
cat_id smallint(5) unsigned not null,
user_id int(11) unsigned not null,
views int(11) unsigned not null default 0,
status tinyint(1) unsigned not null default 0,
primary key (id),
unique key (title,cat_id),
foreign key (cat_id) references category (id) on delete restrict on update cascade,
foreign key (user_id) references users (id) on delete cascade on update cascade
) engine=innodb default charset=utf8;
but i dont know why i cant query viewers table i dont know why
create table viewers (
id int(11) unsigned not null auto_increment,
post_id bigint(20) unsigned not null,
primary key (id),
foreign key (post_id) references posts (id) on delete cascade
) engine=innodb default charset=utf8;
please help :)
Please try removing fks
Most commonly it is because of different properties.
Check
if id of post table is having same properties as of this? (here bigint)
Other possibilities could be
it isn't innodb engine for other table.
Names of fks are not unique

Problem in Saving Multi Level Models in YII

My Table structure for user and his adress detail is as follows
CREATE TABLE tbl_users (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
loginname varchar(128) NOT NULL,
enabled enum("True","False"),
approved enum("True","False"),
password varchar(128) NOT NULL,
email varchar(128) NOT NULL,
role_id int(20) NOT NULL DEFAULT '2',
name varchar(70) NOT NULL,
co_type enum("S/O","D/O","W/O") DEFAULT "S/O",
co_name varchar(70),
gender enum("MALE","FEMALE","OTHER") DEFAULT "MALE",
dob date DEFAULT NULL,
maritalstatus enum("SINGLE","MARRIED","DIVORCED","WIDOWER") DEFAULT "MARRIED",
occupation varchar(100) DEFAULT NULL,
occupationtype_id int(20) DEFAULT NULL,
occupationindustry_id int(20) DEFAULT NULL,
contact_id bigint(20) unsigned DEFAULT NULL,
signupreason varchar(500),
PRIMARY KEY (id),
UNIQUE KEY loginname (loginname),
UNIQUE KEY email (email),
FOREIGN KEY (role_id) REFERENCES tbl_roles (id),
FOREIGN KEY (occupationtype_id) REFERENCES tbl_occupationtypes (id),
FOREIGN KEY (occupationindustry_id) REFERENCES tbl_occupationindustries (id),
FOREIGN KEY (contact_id) REFERENCES tbl_contacts (id)
) ENGINE=InnoDB;
CREATE TABLE tbl_contacts (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
contact_type enum("cres","pres","coff"),
address varchar(300) DEFAULT NULL,
landmark varchar(100) DEFAULT NULL,
district_id int(11) DEFAULT NULL,
city_id int(20) DEFAULT NULL,
state_id int(20) DEFAULT NULL,
pin_id bigint(20) unsigned DEFAULT NULL,
area_id bigint(20) unsigned DEFAULT NULL,
po_id bigint(20) unsigned DEFAULT NULL,
phone1 varchar(20) DEFAULT NULL,
phone2 varchar(20) DEFAULT NULL,
mobile1 varchar(20) DEFAULT NULL,
mobile2 varchar(20) DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (district_id) REFERENCES tbl_districts (id),
FOREIGN KEY (city_id) REFERENCES tbl_cities (id),
FOREIGN KEY (state_id) REFERENCES tbl_states (id)
) ENGINE=InnoDB;
CREATE TABLE tbl_states (
id int(20) NOT NULL AUTO_INCREMENT,
name varchar(70) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;
CREATE TABLE tbl_districts (
id int(20) NOT NULL AUTO_INCREMENT,
name varchar(70) DEFAULT NULL,
state_id int(20) DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (state_id) REFERENCES tbl_states (id)
) ENGINE=InnoDB;
CREATE TABLE tbl_cities (
id int(20) NOT NULL AUTO_INCREMENT,
name varchar(70) DEFAULT NULL,
district_id int(20) DEFAULT NULL,
state_id int(20) DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (district_id) REFERENCES tbl_districts (id),
FOREIGN KEY (state_id) REFERENCES tbl_states (id)
) ENGINE=InnoDB;
The relationship is as follows
User has multiple contacts i.e Permanent Address, Current Address, Office Address.
Each Contact has state and City.
User->Contact->state like this
How to save models of this structure in one go.
Please provide a reply ASAP
I believe I had a similar issue saving a multi leveled model with many foreign keys. It doesn't seem that it can easily be saved in 'one go'...
Take a look at my solution here
You might have a look at gii-templates extension on google code -
gii templates on google code
->> It might be documented better, but it attempts to do what you need.
(It might save you some time if you implement it well, but I'd even consider hand-coding it
if I were you.)

Categories