Foreign Key Showing Error On Insert

Hello All,

I have the database like this

 ===  Group ===




 === Member ===


 group_id (FK)





Here I have used multimodel to get the values of Member into Group.So here in Group I can easily make create,update and delete for both models in a single form. Now as I am going to only Member model and trying to save something it is showing error like this

 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 (`tbl_groupapp`.`tbl_member`, CONSTRAINT `FK_member_group` FOREIGN KEY (`group_id`) REFERENCES `tbl_group` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `tbl_member` (`firstname`, `lastname`, `gender`, `membersince`) VALUES (:yp0, :yp1, :yp2, :yp3) 


Then I found that Member model wants to save its group_id which I am not saving.So I made changes in my member’s form file and I changed my code like this

 <?php echo $form->dropDownList($model,'group_id',CHtml::listData(Group::model()->findAll(),'id','name'),array('empty'=>'Select Group'));?>


But here I am getting all the ids as dropdown list.Now I want that the field for group_id will be hidden and it will take its only corresponding id from the Group model.So that one will use only Member model without putting any attributes for group_id,it will take the id from the corresponding table.So how to do this?Any help and suggestions will be highly appriciable.


  1. If id in Group table never can be NULL, you can’t save single row in Member table. You can save it only if you have corresponded data in Group table.

  2. Make filters.

Hi @Jampire, Thanks for the quick reply. So can you tell me how to use filter in Member model?I am newbie to the Yii.

Is it possible to get the id of Group in Member’s group_id for the corresponding id?If yes then how?