chrischa
(Christopherque)
January 9, 2015, 6:42am
1
Hi all, im new to the Yii and Object oriented, I received an error when displaying the list view.Can u guys help me? Thanks.
Error :Fatal error: Call to a member function getAttributeLabel() on array in C:\xampp\htdocs\TESTING\protected\views\branch\_myview.php on line 3
Codes:
a)BranchController.php
public function actionshow($id)
{
$branch_name=branch::model()->findByPk($id)->branch_name;
$pageTitle = "Branch - $branch_name";
$count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM branch')->queryScalar();
$sql = 'SELECT * FROM branch WHERE branch_name = "Kota Kinabalu"';
$dataProvider=new CSqlDataProvider($sql,array(
'keyField'=>'id',
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array('id'),
'defaultOrder'=>array('id'=>false),
),
'pagination'=>array(
'pageSize'=>10,
),
));
$this->render('myview', array(
'dataProvider'=>$dataProvider,
'pageTitle'=>$pageTitle,));
}
b)myview.php
<?php
$this->breadcrumbs=array(‘branch’,);
?>
<?php
$this->widget('zii.widgets.CListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_myview',
));
?>
c)_myview
<div class="myview">
<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::encode($data->id); ?>
<br />
</div>
You are using a CSqlDataProvider, so you haven’t instances of your model, but only raw data.
So in _myview.php you must add an object from branch model to get all methods:
<?php $obj = new branch(); ?>
<div class="myview">
<b><?php echo CHtml::encode($obj->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::encode($data->id); ?>
<br />
</div>
Otherwise you can change CSqlDataProvider in ActiveDataProvider, so you should have an array of ActiveRecord, instead of an array of raw data.
chrischa
(Christopherque)
January 10, 2015, 1:58am
3
You are using a CSqlDataProvider, so you haven’t instances of your model, but only raw data.
So in _myview.php you must add an object from branch model to get all methods:
<?php $obj = new branch(); ?>
<div class="myview">
<b><?php echo CHtml::encode($obj->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::encode($data->id); ?>
<br />
</div>
Otherwise you can change CSqlDataProvider in ActiveDataProvider, so you should have an array of ActiveRecord, instead of an array of raw data.
Appreciate and thanks for the reply as i didnt receive any error anymore.Let say that I try this way,
I have a problem that It didnt show the value eventhough the return row is correct as I already double check.Could it be that the CSqlDataProvider didnt sent fully to the view?
[b]
FYI:I change the _myview.php:[/b]
<div class="myview">
<b><?php echo CHtml::encode($obj->getAttributeLabel(‘id’)); ?>:</b>
<?php echo CHtml::encode($obj->id); ?>
<br />
</div>
INTO:
<div class="myview">
<b><?php echo CHtml::encode($obj->getAttributeLabel(‘branch_name’)); ?>:</b>
<?php echo CHtml::encode($obj->branch_name); ?>
<br />
</div>
If you change into
<div class="myview">
<b><?php echo CHtml::encode($obj->getAttributeLabel('branch_name')); ?>:</b>
<?php echo CHtml::encode($obj->branch_name); ?>
<br />
</div>
obviously $obj is a fresh object, so it hasn’t any data.
In my example i’ve used for data to display $data object.
See again my example:
<?php $obj = new branch(); ?>
<div class="myview">
<b><?php echo CHtml::encode($obj->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::encode($data->id); ?>
<br />
</div>
Attribute label is form $obj, but data displayed is from $data.
chrischa
(Christopherque)
January 12, 2015, 1:33am
5
If you change into
<div class="myview">
<b><?php echo CHtml::encode($obj->getAttributeLabel('branch_name')); ?>:</b>
<?php echo CHtml::encode($obj->branch_name); ?>
<br />
</div>
obviously $obj is a fresh object, so it hasn’t any data.
In my example i’ve used for data to display $data object.
See again my example:
<?php $obj = new branch(); ?>
<div class="myview">
<b><?php echo CHtml::encode($obj->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::encode($data->id); ?>
<br />
</div>
Attribute label is form $obj, but data displayed is from $data.
Thanks.It solve my problem.