CListView and CArrayDataProvider: getAttributes

I successfully created a CArrayDataProvider with the following lines of code:




$dataProvider=new CArrayDataProvider($myArray, array(

            'id'=>'ID',

            'sort'=>array(

                'attributes'=>array(

         			'ID', Type', 'Name', 'Brand','CreateDate'

                ),

            ),

            'pagination'=>array(

                'pageSize'=>10,

            ),

        ));

This gets passed on to the view correctly where I can see the content of the CArrayDataProvider.

In this view I call:




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

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); ?>

And from there on I don’t know what’s going on. I simply adapted the_view.php which gets generated by gii when you use the CRUD generator. So I end up with a _view.php file like this:





<div class="view">

	<b><?php echo CHtml::encode($data->getAttributeLabel('ID')); ?>:</b>

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

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('Type')); ?>:</b>

	<?php echo CHtml::encode($data->Type); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('Name')); ?>:</b>

	<?php echo CHtml::encode($data->Name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('Brand')); ?>:</b>

	<?php echo CHtml::encode($data->Brand); ?>

	<br />


<br class="Apple-interchange-newline">	<b><?php echo CHtml::encode($data->getAttributeLabel('CreateDate')); ?>:</b>

	<?php echo CHtml::encode($data->CreateDate); ?>

	<br />

</div>

If I open the website I get the following error:

However I can see attributes in the CArrayDataProvider:




    ...

    [_sort:private] => CSort Object

        (

            [multiSort] => 

            [modelClass] => 

            [attributes] => Array

                (

                    [0] => ID

                    [1] => Type

                    [2] => Name

                    [3] => Brand

                    [4] => CreateDate

                )

                ...



Can you please help me understand where my mistakes are?

Hi, jrn

getAttributeLabel is a CActiveRecord method. So you have to create the CArrayDataProvider with the raw data of an array of CActiveRecord objects when you want to use that method.

Probably you are currently creating your CArrayDataProvider with the raw data of an array of query results of DAO, just as illustrated in the reference.

CArrayDataProvider

Try the second (comment-outed) method instead of the first.

Or, forget the getAttributeLabel method and provide the labels by yourself.

Hi softark,

thank you very much for this answer!

This is indeed the problem I am facing. I am creating the CArrayDataProvider with raw data. Since I am using stored procedures I am not able to use the method you suggested. However I will simply not use the getAttributeLabel method as you mentioned.

What would be the best way to access the labels in _view.php?

Since I have to pass $model to my view index.php where I call the CListView:




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

        'dataProvider'=>$dataProvider,

        'itemView'=>'_view',

)); ?>

Another issue I ran into was that I forgot to declare a keyItem in the CArrayDataProvider like so:




$dataprovider = new CArrayDataProvider($list, array(

	'keyField' => 'some_id',         // PRIMARY KEY

	...

));

Hi, jrn

‘getAttributeLabel’ is not a static function, so it requires an object instance.

Um, I think that you can write some static function in your model that returns the attribute label, with some modifications to the existing ‘attributeLabels’ function.




public static function getAttrLabel($attr)

{

    return self::_attr_labels[strtolower($attr)];

}

public function attributeLabels()

{

    return self::_attr_labels[$attr];

    // return array(

    //    'id'=>'ID',

    //    'name'=>'Name',

    //    ...

    // );

}

private function _attr_labels()

{

    return array(

        'id'=>'ID',

        'name'=>'Name',

        ...

    );

}


// in your view.php


<b><?php echo CHtml::encode(YourModel::getAttrLabel('Type')); ?>:</b>




Hi softark.

this lines is not working for me. It says:

I tried to replace " [ ] " with " ( ) ", but it’s also not working.

how can I solve this error? :mellow:

Thanks.