Is this a bug in CAdvancedArBehavior?

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'

		)

	);

		

}



The message is clear… you are trying to insert in tbl_painting_color a value "empty" but that value does not exist in the tbl_color table.

mdomba,

This only happens if I remove all of the colors. If I leave at least one color during the update, I’m not getting any errors.

Anyone else can shed a light on this?

If you are updating a record and deselect all colors then the previous colors should be deleted from the table and nothing inserted… but it seems that at that time the problematic record with the empty color field gets inserted…

I explained you what the error means… now it’s up to you to see why that happens…

can’t help you more as I did not use that extension…

In version 0.3 of CAdvancedArBehavior:

open CAdvancedArBehavior.php

find the line (160):


$this->owner->$key = array($this->owner->$key);

and add below it:


else if (is_array($this->owner->$key)&& $this->owner->$key != array())

This should solve your problem

Thank you for your solution but I’ve decided to use another extension due to poor performance of CAdvancedArBehavior

Could you tell wich extension you use to manage that ?

Thanks