Search with DAO and then formatting it with CGridView(+CArrayDataProvider)

Hey,

I am trying to build a search query with DAO and then returing the result by a CArrayDataProvider.

But I’m getting a error message witch says “Trying to get property of non-object”.

I guess this has to do with the "CArrayDataProvider".

Code from my Model:




public function getSearchAsQuery()

	{

		

		$par=array(	'CustomerNr' => $this->CustomerNr,

					'CustomerName' => $this->CustomerName,

					'CustomerLocation' => $this->CustomerLocation,

					'CustomerCountry' => $this->CustomerCountry,

					'CustomerName2' => $this->CustomerName2,

					'CustomerPostcode' => $this->CustomerPostcode,

					'CustomerRegion' => $this->CustomerRegion,

					'CustomerStreet' => $this->CustomerStreet,

					'CustomerPhone' => $this->CustomerPhone,

					'CustomerFax' => $this->CustomerFax,

					'CustomerEmail' => $this->CustomerEmail,

					'CustomerTimestamp' => $this->CustomerTimestamp);

		

		/*

		$where = array();

		$where[] = "and";

		$rows = array_keys($par);

		for($i = 0; $i < count($rows); $i++){

			if($par[$rows[$i]] != NULL)

				$where[] = $rows[$i]."='".$par[$rows[$i]]."'";

		}

		*/

		

		$where = array();

		$where[] = "and";

		$rows = array_keys($par);

		for($i = 0; $i < count($rows); $i++){

			if($rows[$i] == "CustomerNr"){

				$where[] = $rows[$i]."='".$par[$rows[$i]]."'";

			}else{

				$where[] = $rows[$i]."='".$par[$rows[$i]]."'";

			}

		}

		

				

		$reader = Yii::app()->db->createCommand()

		->select('*')

		->limit(100)

		->from('customersap')

		->where($where)

		->queryAll();

		

		$rawData=$reader;

		

	

		return new CArrayDataProvider($rawData, array(

				'pagination'=>array(

						'pageSize'=>10,

				),

		));	

	}



Code from my View:




<?php

$this->breadcrumbs=array(

	'Customer Saps'=>array('index'),

	'Manage',

);


$this->menu=array(

	array('label'=>'List CustomerSAP', 'url'=>array('index')),

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

);


Yii::app()->clientScript->registerScript('search', "

$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});

$('.search-form form').submit(function(){

	$.fn.yiiGridView.update('customer-sap-grid', {

		data: $(this).serialize()

	});

	return false;

});

");

?>


<h1>Manage Customer Saps</h1>


<p>

You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>

or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.

</p>


<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>

<div class="search-form" style="display:none">

<?php $this->renderPartial('_search',array(

	'model'=>$model,

)); ?>

</div><!-- search-form -->


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'customer-sap-grid',

	'dataProvider'=>$model->getSearchAsQuery(),	

	//'dataProvider'=>$model->search(),

	//'filter'=>$model,

	'columns'=>array(

		'CustomerNr',

		'CustomerName',

		'CustomerLocation',

		'CustomerCountry',

		'CustomerName2',

		'CustomerPostcode',

		/*

		'CustomerRegion',

		'CustomerStreet',

		'CustomerPhone',

		'CustomerFax',

		'CustomerEmail',

		'CustomerTimestamp',

		*/

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



Thanks in advance!

Okay found that CButtonColumn doesn’t work with non-object data-provider.

So without CButtonColumn I can atleast do a "SELECT *".

The problem now seems to be that i can’t get access to model variables and therefor to my search input…

Is


'CustomerNr' => $this->CustomerNr

not the right way to access them?

I solved this. There was a probleme with my if.

For those who are interested:


public function getSearchAsQuery()

	{	

		$par=array(	'CustomerNr' => $this->CustomerNr,

					'CustomerName' => $this->CustomerName,

					'CustomerLocation' => $this->CustomerLocation,

					'CustomerCountry' => $this->CustomerCountry,

					'CustomerName2' => $this->CustomerName2,

					'CustomerPostcode' => $this->CustomerPostcode,

					'CustomerRegion' => $this->CustomerRegion,

					'CustomerStreet' => $this->CustomerStreet,

					'CustomerPhone' => $this->CustomerPhone,

					'CustomerFax' => $this->CustomerFax,

					'CustomerEmail' => $this->CustomerEmail,

					'CustomerTimestamp' => $this->CustomerTimestamp);

			

		$where = array();

		$where[] = "and";

		$rows = array_keys($par);

		for($i = 0; $i < count($rows); $i++)

		{

				if($par[$rows[$i]] != "")

					$where[] = $rows[$i]."='".$par[$rows[$i]]."'";

		}

		if (count($where) <= 1)

				$where[] = "customernr IS NOT NULL";

		

		/*

		echo count($where);

		print_r($where);

		*/

				

		$reader = Yii::app()->db->createCommand()

		->select('*')

		->limit(100)

		->from('customersap')

		->where($where)

		->queryAll();

		

		

		$rawData=$reader;

		

		//$rawData=Yii::app()->db->createCommand('SELECT * FROM customersap')->queryAll();

		return new CArrayDataProvider($rawData, array(

				'id'=>'sapDataProvider',

				'keyField'=>'CustomerNr',

				'sort'=>array(

 			       'attributes'=>array(

             			'CustomerNr',

						'CustomerName',

						'CustomerLocation',

						'CustomerCountry',

						'CustomerName2',

						'CustomerPostcode',

						'CustomerRegion',

						'CustomerStreet',

						'CustomerPhone',

						'CustomerFax',

						'CustomerEmail',

						'CustomerTimestamp',

        			),

    			),

				'pagination'=>array(

						'pageSize'=>10,

				),

		));	

	}

Hmm, I’m still not happy with my CButtonColumn.

I can’t get it to work…

I added the KeyField to my ArrayDataProvider and also changed the viewButtonUrl.

But still gives me an error.

Here is my code again, maybe someone can help me:

Model


<?php


/**

 * This is the model class for table "CustomerSAP".

 *

 * The followings are the available columns in table 'CustomerSAP':

 * @property string $CustomerNr

 * @property string $CustomerName

 * @property string $CustomerLocation

 * @property string $CustomerCountry

 * @property string $CustomerName2

 * @property string $CustomerPostcode

 * @property string $CustomerRegion

 * @property string $CustomerStreet

 * @property string $CustomerPhone

 * @property string $CustomerFax

 * @property string $CustomerEmail

 * @property string $CustomerTimestamp

 */

class CustomerSAP extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return CustomerSAP the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'CustomerSAP';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('CustomerNr', 'required'),

			array('CustomerNr, CustomerName, CustomerLocation, CustomerCountry, CustomerName2, CustomerPostcode, CustomerRegion, CustomerStreet, CustomerPhone, CustomerFax, CustomerEmail, CustomerTimestamp', 'length', 'max'=>255),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('CustomerNr, CustomerName, CustomerLocation, CustomerCountry, CustomerName2, CustomerPostcode, CustomerRegion, CustomerStreet, CustomerPhone, CustomerFax, CustomerEmail, CustomerTimestamp', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	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(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'CustomerNr' => 'Customer Nr',

			'CustomerName' => 'Customer Name',

			'CustomerLocation' => 'Customer Location',

			'CustomerCountry' => 'Customer Country',

			'CustomerName2' => 'Customer Name2',

			'CustomerPostcode' => 'Customer Postcode',

			'CustomerRegion' => 'Customer Region',

			'CustomerStreet' => 'Customer Street',

			'CustomerPhone' => 'Customer Phone',

			'CustomerFax' => 'Customer Fax',

			'CustomerEmail' => 'Customer Email',

			'CustomerTimestamp' => 'Customer Timestamp',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	 

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('CustomerNr',$this->CustomerNr,true);

		$criteria->compare('CustomerName',$this->CustomerName,true);

		$criteria->compare('CustomerLocation',$this->CustomerLocation,true);

		$criteria->compare('CustomerCountry',$this->CustomerCountry,true);

		$criteria->compare('CustomerName2',$this->CustomerName2,true);

		$criteria->compare('CustomerPostcode',$this->CustomerPostcode,true);

		$criteria->compare('CustomerRegion',$this->CustomerRegion,true);

		$criteria->compare('CustomerStreet',$this->CustomerStreet,true);

		$criteria->compare('CustomerPhone',$this->CustomerPhone,true);

		$criteria->compare('CustomerFax',$this->CustomerFax,true);

		$criteria->compare('CustomerEmail',$this->CustomerEmail,true);

		$criteria->compare('CustomerTimestamp',$this->CustomerTimestamp,true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

	

	//FRANZ SUCHABFRAGE

	

	public function getSearchAsQuery()

	{	

		$par=array(	'CustomerNr' => $this->CustomerNr,

					'CustomerName' => $this->CustomerName,

					'CustomerLocation' => $this->CustomerLocation,

					'CustomerCountry' => $this->CustomerCountry,

					'CustomerName2' => $this->CustomerName2,

					'CustomerPostcode' => $this->CustomerPostcode,

					'CustomerRegion' => $this->CustomerRegion,

					'CustomerStreet' => $this->CustomerStreet,

					'CustomerPhone' => $this->CustomerPhone,

					'CustomerFax' => $this->CustomerFax,

					'CustomerEmail' => $this->CustomerEmail,

					'CustomerTimestamp' => $this->CustomerTimestamp);

			

		$where = array();

		$where[] = "and";

		$rows = array_keys($par);

		for($i = 0; $i < count($rows); $i++)

		{

				if($par[$rows[$i]] != "")

					$where[] = $rows[$i]."='".$par[$rows[$i]]."'";

		}

		if (count($where) <= 1)

				$where[] = "customernr IS NOT NULL";

		

		/*

		echo count($where);

		print_r($where);

		*/

				

		$reader = Yii::app()->db->createCommand()

		->select('*')

		//->limit(100)

		->from('customersap')

		->where($where)

		->queryAll();

		

		

		$rawData=$reader;

		

		//$rawData=Yii::app()->db->createCommand('SELECT * FROM customersap')->queryAll();

		return new CArrayDataProvider($rawData, array(

				'id'=>'sapDataProvider',

				'keyField'=>'CustomerNr',

				'sort'=>array(

 			       'attributes'=>array(

             			'CustomerNr',

						'CustomerName',

						'CustomerLocation',

						'CustomerCountry',

						'CustomerName2',

						'CustomerPostcode',

						'CustomerRegion',

						'CustomerStreet',

						'CustomerPhone',

						'CustomerFax',

						'CustomerEmail',

						'CustomerTimestamp',

        			),

    			),

				'pagination'=>array(

						'pageSize'=>10,

				),

		));	

	}

}

Controller


should not be that important

View


<?php

$this->breadcrumbs=array(

	'CustomerSAP'=>array('index'),

	'Manage',

);


$this->menu=array(

	array('label'=>'List CustomerSAP', 'url'=>array('index')),

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

);


Yii::app()->clientScript->registerScript('search', "

$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});

$('.search-form form').submit(function(){

	$.fn.yiiGridView.update('customer-sap-grid', {

		data: $(this).serialize()

	});

	return false;

});

");

?>


<h1>FEDIS CustomerSAP</h1>


<p>

You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>

or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.

</p>


<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>

<div class="search-form" style="display:none">

<?php $this->renderPartial('_search',array(

	'model'=>$model,

)); ?>

</div><!-- search-form -->


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'customer-sap-grid',

	'dataProvider'=>$model->getSearchAsQuery(),	

	//'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'CustomerNr',

		'CustomerName',

		'CustomerLocation',

		'CustomerCountry',

		'CustomerName2',

		'CustomerPostcode',

		/*

		'CustomerRegion',

		'CustomerStreet',

		'CustomerPhone',

		'CustomerFax',

		'CustomerEmail',

		'CustomerTimestamp',

		

		array(

			'class'=>'CButtonColumn',

		),

		*/

		array(

			'class'=>'CButtonColumn',

			'viewButtonUrl'=>'Yii::app()->createUrl("/CustomerSAP/view", array("id"=>$data[\'CustomerNr\']))',

		),

		

		

	),

)); ?>