Need help with database design and Relational Active Record

Hi,

I am new to yii and php. I am testing the framework and have been stuck for days. Below is my SQL for the database:

CREATE TABLE IF NOT EXISTS tbl_project (

id int(11) NOT NULL AUTO_INCREMENT,

description varchar(500) DEFAULT NULL,

client_id int(11) DEFAULT NULL,

status_id int(11) DEFAULT NULL,

job_number varchar(10) DEFAULT NULL,

date datetime DEFAULT NULL,

PRIMARY KEY (id),

KEY client_id (client_id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

CREATE TABLE IF NOT EXISTS tbl_client (

id int(11) NOT NULL AUTO_INCREMENT,

firstname varchar(20) NOT NULL,

lastname varchar(20) NOT NULL,

company varchar(30) NOT NULL,

phone varchar(10) NOT NULL,

email varchar(50) NOT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

ALTER TABLE tbl_project

ADD CONSTRAINT tbl_project_client FOREIGN KEY (client_id) REFERENCES tbl_client (id) ON DELETE CASCADE;

I have run the model and crud generators and everything works properly and I can access client properties with Relational AR. However when I delete a client from the database, it also deletes the associated projects. This is not the behavior I expected. I am sure it is nothing but a database design issue, but I can not figure it out.

Any ideas?

Thanks,

Darryl

Hi, just remove the tbl_project_client foreign key

mysql automatically removes all projects associated with a user to save data integrity

Actually it’s the “ON DELETE CASCADE” that is doing just that… check the MySql documentation for more information

Thanks for the replies. I changed the CASCADE ON DELETE to SET NULL. Now things seem to be working properly.

I’m sure I will be back with more questions.