Looks like CAdvancedArBehavior is trying to insert an empty row to the association table which is causing the database integrity violation.
Has anyone encountered a similar situation before?
Here is what I did before generating this error;
* Add a few colors to Painting model via check box list in the form view and save.
(x) Yellow
(x) Blue
( ) Black
( ) White
(x) Red
( ) Gray
* Remove all of the previously selected colors and save.
( ) Yellow
( ) Blue
( ) Black
( ) White
( ) Red
( ) Gray
CDbException that I'm getting;
Quote
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 (`mydb`.`tbl_painting_color`, CONSTRAINT `FK_pcol_color` FOREIGN KEY (`color_id`) REFERENCES `tbl_color` (`id`) ON DELETE CASCADE). The SQL statement executed was: insert into tbl_painting_color (painting_id, color_id) values ('1', '')
Database Tables
************** ********************** **************** * tbl_color * * tbl_painting_color * * tbl_painting * ************** ********************** **************** * id *------------* *---------------* id * * * * color_id * * * * * * painting_id * * * ************** ********************** ****************
Controller
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Painting']))
{
$model->attributes=$_POST['Painting'];
$model->colors = $_POST['Painting']['colorIds'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
View
<div class="row">
<?php echo $form->labelEx($model, 'colors'); ?>
<?php echo $form->checkBoxList($model, 'colorIds', CHtml::listData(Color::model()->findAll(), 'id', 'name'), array('labelOptions' => array('style' => 'display:inline'))); ?>
<?php echo $form->error($model, 'colors'); ?>
</div>
Model
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('colors', 'safe'),
);
}
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(
'colors' => array(self::MANY_MANY, 'Color', 'tbl_painting_color(painting_id, color_id)'),
);
}
public function behaviors() {
return array(
'CAdvancedArBehavior' => array(
'class' => 'application.extensions.CAdvancedArBehavior'
)
);
}

Help













