primary and foreign key in yii

Hi everyone. am a new bee to yii framework

I have created two tables with two primary keys and one foreign key (reference of first table) and when I applied gii crud to those tables first one with out fk is ok . But the problem is if the id (10 eg) is not there in first table and trying add the non existing value(eg 10) in referencing table its giving error. I know the error is unless we dont have entries in main table we can enter the values in another table. When I apply crud operations using gii am getting error.

I want to know that how to handle that error.



shaik what error message you getting

I am getting this year when I try to add a group id of which is not there in first table(primary key).

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (testyiiapp.explanationcontent, CONSTRAINT explanationcontent_ibfk_1 FOREIGN KEY (groupid) REFERENCES explanationmaster (id)). The SQL statement executed was: UPDATE explanationcontent SET id=:yp0, groupid=:yp1, sequence=:yp2, exp_text=:yp3 WHERE



Hello everybody,

I am beginner in yii framework.

I need help for implementing relationship between table in yii please give me the solution using example.

shaik did you try this command in phpmyadmin or something like this ?

@sunny welcome to forum, two links for You:

first to read:

Hi Shaikh,

you are trying to insert into your child table when u dont have related entry in your parent table. try to insert that parent id in your parent table. then you can add the same key id in your child table.

Hi Sunny,

Welcome to Yii Family. if you can tell us what you want to achieve…that’d be easy for someone to help you…

however find this link it helps…