I’m getting a CDbException while trying to update a checkBoxList. I use CAdvancedArBehavior to save the many to many relation.
[b]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?[/b]
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;
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'
)
);
}