Yii Framework Forum: Is this a bug in CAdvancedArBehavior? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Is this a bug in CAdvancedArBehavior? Rate Topic: -----

#1 User is offline   oxo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 16-March 12

Posted 06 May 2012 - 04:43 AM

I'm getting a CDbException while trying to update a checkBoxList. I use CAdvancedArBehavior to save the many to many relation.

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'
		)
	);
		
}

0

#2 User is online   Maurizio Domba 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,229
  • Joined: 12-October 09
  • Location:Croatia

Posted 06 May 2012 - 05:26 AM

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.
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   oxo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 16-March 12

Posted 06 May 2012 - 05:29 AM

View Postmdomba, on 06 May 2012 - 05:26 AM, said:

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.
0

#4 User is offline   oxo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 16-March 12

Posted 07 May 2012 - 02:14 AM

Anyone else can shed a light on this?
0

#5 User is online   Maurizio Domba 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,229
  • Joined: 12-October 09
  • Location:Croatia

Posted 07 May 2012 - 03:11 AM

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...
Find more about me.... btw. Do you know your WAN IP?
0

#6 User is offline   hmsdexter 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 07-May 12

Posted 07 May 2012 - 06:58 AM

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
0

#7 User is offline   oxo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 16-March 12

Posted 07 May 2012 - 09:57 AM

View Posthmsdexter, on 07 May 2012 - 06:58 AM, said:

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users