Clistview With Carraydataprovider And Xml

Hi, I am implementing a ClistView widget with a CArrayDataProvider. The original data comes from an XML document that I load using simplexml_load_file. Then I convert the SimpleXML object to an array and use that array as $rawData for CArrayDataProvider. Everything works fine, except for Sorting. Below my code:

Controller:




public function actionResults()

	{

		$model= new Search('results'); //Search is a CFormModel class

		

		if(isset($_GET['Search']))

    		$model->attributes=$_GET['Search'];

			if($model->validate())

			{

				$yachtResults = new YachtResult(array(

					$model->location,

					$model->guests,

					$model->typeofYacht,

					$model->arrivalDate,

					$model->departureDate,

					$model->budget,

				));		


				$rawData = $yachtResults->getRawData();

				

				$dataProvider=new CArrayDataProvider($rawData, array(

					'id'=>'id',

    				'sort'=>array(

        				'attributes'=>array(

             				'id',

							'name',

							'size',

							'yearBuilt',

							'pax',

							'cabins',

							'lowPrice',

						),

    				),

					'pagination'=>array(

			    	    'pageSize'=>10,

    				),

				));

				

				$this->render('results', array(

					'model'=>$model,

					'dataProvider'=>$dataProvider,

					'yachtResults'=>$yachtResults,	

					'rawData'=>$rawData,	

				));

			}	

			else

			{

				$this->render('index', array(

					'model'=>$model	

				));

			}

			

	}



YachtResult class:




class YachtResult

{

	public function __construct($query) 

	{ 

    	$this->query =  "subin=1".

						"&user=1799".

						"&ylocations=".$query[0].

						"&guests=".$query[1].

						"&boattype=".$query[2].

						"&startday=".$query[3].

						"&startmonth=".$query[3].

						"&startyear=".$query[3].

						"&endday=".$query[4].

						"&endmonth=".$query[4].

						"&endyear=".$query[4].

						"&priceto=".$query[5]; 

		$this->xmlstring = 'someurl/xml.php?'.$this->query;						

		$this->yachts = simplexml_load_file($this->xmlstring);

	}


	public function getRawData()

	{

			foreach($this->yachts as $yacht)

			{

				$rawData[] = array(

					'id'=>$yacht->yachtId,

					'name'=>$yacht->yachtName,

					'size'=>$yacht->size,

					'yearBuilt'=>$yacht->yachtYearBuilt,

					'pax'=>$yacht->yachtPax,

					'cabins'=>$yacht->yachtCabins,

					'type'=>$yacht->yachtType,

					'crew'=>$yacht->yachtCrew,

					'lowPrice'=>$yacht->yachtLowPrice,

					'highPrice'=>$yacht->yachtHighPrice,

					'ebrochureThumb'=>$yacht->yachtEbrochureThumb,

					'ebrochurePic'=>$yacht->yachtEbrochurePic,

					'brokerWeb'=>$yacht->yachtBrokerWeb,

					'video'=>$yacht->yachtVideo,

					);

			}

			return $rawData;	

	}


	public function attributeLabels()

	{

		return array(

			'id'=>'ID',

			'name'=>'Name',

			'size'=>'Size',

			'yearBuilt'=>'Year Built',

			'pax'=>'Guests',

			'cabins'=>'Cabins',

			'type'=>'Type',

			'crew'=>'Crew Members',

			'lowPrice'=>'From',

			'highPrice'=>'To',

			//'ebrochureThumb'=>

			//'ebrochurePic'=>

			'brokerWeb'=>'Broker Web',

			'video'=>'Video',

		);

	}

	

}



results view:




<?php

/* @var $this SearchController */

/* @var $model Search */

/* @var $yachts SearchController */


$this->menu=array(

	array('label'=>'Create YachtSearch', 'url'=>array('create')),

	array('label'=>'Manage YachtSearch', 'url'=>array('admin')),

);

?>

<h1>Search Results:</h1>

<div class="form">




<?php 

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

	'dataProvider'=>$dataProvider,

	'itemView'=>'_indResults',

	'viewData'=>$yachtResults->attributeLabels(),

	'sortableAttributes'=>array(

        'id'=>'ID',

		'name'=>'Name',

		'size'=>'Size',

		'yearBuilt'=>'Year Built',

		'pax'=>'Guests',

		'cabins'=>'Cabins',

		'lowPrice'=>'From',

	),

)); ?>



_indResults:




<div class="view">


	<b><?php echo CHtml::encode($id); ?>:</b>

	<?php echo $data['id']; ?>

	<br />	

    

    <b><?php echo CHtml::encode($name); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data['name']), array('view', 'id'=>$data['id'])); ?>

	<br />


	<b><?php echo CHtml::encode($size); ?>:</b>

	<?php echo CHtml::encode($data['size']); ?>

	<br />


	<b><?php echo CHtml::encode($yearBuilt); ?>:</b>

	<?php echo CHtml::encode($data['yearBuilt']); ?>

	<br />

    

    <b><?php echo CHtml::encode($pax); ?>:</b>

	<?php echo CHtml::encode($data['pax']); ?>

	<br />

    

    <b><?php echo CHtml::encode($cabins); ?>:</b>

	<?php echo CHtml::encode($data['cabins']); ?>

	<br />

    

    <b><?php echo CHtml::encode($type); ?>:</b>

	<?php echo CHtml::encode($data['type']); ?>

	<br />

    

    <b><?php echo CHtml::encode($crew); ?>:</b>

	<?php echo CHtml::encode($data['crew']); ?>

	<br />

    

    <b><?php echo CHtml::encode($lowPrice); ?>:</b>

	<?php echo $data['lowPrice']; ?>

	<br />

    

    <b><?php echo CHtml::encode($highPrice); ?>:</b>

	<?php echo CHtml::encode($data['highPrice']); ?>

	<br />


</div>



Thanks in advance!

German

I have never used CArrayDataProvider, but it seems that you didn’t call CArrayDataProvider::sortData.

Please take a look at it and let me know if this helps.

Hi Mentel, thanks for your reply. I used a $rawData array made by hand to put to CArrayDataProvider and everything works fine. I believe the problem is when I create the array from the simpleXLMElement, but can’t find the problem!!!

Thanks,

German

My bad, I’m sorry, that method is protected.

Please put on pastie.org a var_export of that array so we can help you better.

Mentel, thank you very much! You helped me. I realized the array I was building was no filled with strings but with SimpleXMLElements. I changed to DOMDocument for parsing my XML as you can do more things. With that I built my array with strings (not objects) and the widget worked OK.

Thanks again, I hope it helps to someone else.

German

I’m happy I was able to help you.

Thanks for giving feedback. As you said, it may be useful for other people.