Why won't the multiselect value update when no values are selected?

Hi - I am not a seasoned php developer so please be patient.

I am using the Yii Application Development Book to create an application in Yii. Things are moving along just great and I feel the need to congratulate the development team and the community members for a great framework.

At this moment I am having a problem figuring out the following:

I have an Active Record model: Processes pointing to a table processes. There is a db column called process_prerequisites of type varchar where I store a comma delimited (’, ') string representing an array of process_ids, again entries in the same table.

In my _form I use the following code:




    <!-- Multiselect widget -->

	<?php $this->widget(

	        'application.extensions.widgets.emultiselect.EMultiSelect',

	        array('sortable'=>false/true, 'searchable'=>false/true)

	      ); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'process_prerequisites'); ?>		

		<?php echo $form->dropDownList($model,'process_prerequisites', getOtherProcessDropdownValues($model->process_id), array(	'multiple'=>'multiple','key'=>'process_prerequisites', 'class'=>'multiselect')); ?>

		<?php echo $form->error($model,'process_prerequisites'); ?>

	</div>




Since the widget accepts and hands out an array, and my database field accepts a string, I use the following code in my Processes.php model file:





	protected function beforeValidate() 

		{


			if ($this->process_prerequisites == '') {

				$this->setAttribute('process_prerequisites', '');

			} else {

				$this->process_prerequisites=implode(", ",$this->process_prerequisites);

			}


		return parent::beforeValidate(); 

	

	} //End beforeValidate()



and





	public function afterFind()

	{

			if ($this->process_prerequisites == '') {

				$this->setAttribute('process_prerequisites', null);

			} else {

				$this->process_prerequisites=explode(", ",$this->process_prerequisites);

			}

		

		return parent::afterFind();

		

	} //End beforeFind()




Everything works sweetly, except in the case where I update a ‘processes’ record and delete all process_prerequisites values previously added, e.g. by clicking on ‘Remove All’. The update does not work, and my db record holds the previous values.

The update works just fine in all other cases except when all the values are removed. I thought that maybe it’s got something to do with


if ($this->process_prerequisites == '')

so I tried all kinds of things like


if (count($this->process_prerequisites) == 0)

but that doesn’t work either. What is a mystery to me is that when trying to debug by tracking the value of


count($this->process_prerequisites)

,upon save this value reflects the number of process_prerequisites in the multiselect list in all cases except when all values are removed, in which case it simply does not acknowledge any changes. For example if I previously have:

process_prerequisites = 1, 3, 5

and update the record (using _form) so that

process_prerequisites = 1, 3

on save,

code]count($this->process_prerequisites)[/code]

gives me 2 and the change is reflected in the db record just fine.

But if I then remove all the values from process_prerequisites, then the count remains = 2 and the values don’t change.

Any help would be appreciated.

Never mind … Silly me, it’s all about the way that POST handles empty multi-selects. When there are no values selected, there IS no process_prerequisites array, so the record updates with what’s already there.

This is how I solved it:




if (!isset($_POST['Processes']['process_prerequisites']))