listData with empty value

I have a form where the user is able to see the list of vendor to create a new Application,

The problem is that the Vendor is Optional, it means in my dropdown list, i would like to show an empty field that automatically generates en empty value of the VENDOR_ID but yii tells "Trying to get property of non-object"


<?php echo$form->dropDownList(//dropDownList with an empty array for 0,1->0,n relations

			$model,'VENDOR_ID', CHtml::listData(

				Vendor::model()->findAll(),'VENDOR_ID','NAME'

			), 

			array(

					'empty' =>'<no Solder>', 

			)

		);?>



I think i understand the problem, the empty value i create is not of type $model->Vendor that’s why i get an error message, but i don’t know how to solve it.

Help!

Put your Vendor::model().->findAll() into a method of your Vendor model and call this method in the view. In this method check the result of the query and return an empty array if result is empty, otherwise the ActiveRecord elements…

like that??

model\Vendor


public static function findAll()

	{

		if (Vendor::model()->findAll() == null)

			return array();

		else

			return Vendor::model()->findAll();

	}



Found the solutionnnn!!!

The modifications had to be done in the view ,

As a matter of fact , when i looked into the Application table , i saw that my VENDOR_ID was set to void or ‘’

so, the problem was not in the form,

it said "Trying to get property of non-object" because i was searching in my aplication controller




$soldVendorRecords = new Vendor();//VENDOR_ID = SoldBy

		$soldVendorRecords = Vendor::model()->findBySql(

			'SELECT vendor.* FROM application , vendor

			WHERE vendor.VENDOR_ID = application.VENDOR_ID 

			AND application.APPLICATION_ID="'.$id.'" '

		);



of course , there was not any Vendor.VENDOR_ID with a VENDOR_ID = ‘’

so , in my Application view

when i wanted to show the vendor.VENDOR_ID,

like that


$this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'APPLICATION_ID',


		array(

				'label'=>'Solder Vendor Name',

				'value'=>$soldVendorRecords->VENDOR_ID

		),	



i had to check if the Application.VENDOR_ID was equal to ‘’ and if it was true , create a temporary variable




if ( $model->VENDOR_ID == null)//checking if the Vendor is null 

{

	$soldVendorRecords=new Vendor();

	$soldVendorRecords->VENDOR_ID = " ";

}