perochak
(Amjad Mughal)
December 23, 2010, 10:35am
1
Hi dears,
I have the data from a relational object. I want to display it using DataProvide but it is not working.
Any help?
I am getting the data as
$dataProvider=new CActiveDataProvider(
'Categories',
array(
'criteria'=>array(
'with'=>'section',
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
Where as in index.php, when I try to get the attribute or value of the object of the relation, it gives the error.
my code is as
<b><?php echo CHtml::encode($data->getAttributeLabel('section_id')); ?>:</b> // works fine
<?php echo CHtml::encode($data->section_id); ?> // gives error that the object does not have that property defined
How to fix it?
tri
(tri - Tommy Riboe)
December 23, 2010, 11:15am
2
Hi dears,
I have the data from a relational object. I want to display it using DataProvide but it is not working.
Any help?
I am getting the data as
$dataProvider=new CActiveDataProvider(
'Categories',
array(
'criteria'=>array(
'with'=>'section',
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
Where as in index.php, when I try to get the attribute or value of the object of the relation, it gives the error.
my code is as
<b><?php echo CHtml::encode($data->getAttributeLabel('section_id')); ?>:</b> // works fine
<?php echo CHtml::encode($data->section_id); ?> // gives error that the object does not have that property defined
How to fix it?
<?php
foreach($dataProvider->getData() as $data)
echo CHtml::encode($data->section_id);
?>
(should work)
/Tommy
perochak
(Amjad Mughal)
December 23, 2010, 11:43am
3
Thanks dear tri.
But your code did not work.
It gives the same error.
Any help?
tri
(tri - Tommy Riboe)
December 23, 2010, 11:51am
4
How did you declare the models attributes and relationships.
I also noticed you already have $data defined in <?php echo CHtml::encode($data->section_id); ?> (and you told us it works). Something is missing from the view code.
/Tommy
perochak
(Amjad Mughal)
December 24, 2010, 4:00am
5
Hi tri,
I am supplying the data from the CActiveDataProvider with Relational data to the ClistView.
when I tries to get the attribute of the relational data it works fine as
echo CHtml::encode($data->getAttributeLabel('status'));
But When tries to fetch the value of the relational data it gives the error that Categories.section_id is not defined property of the Object Categories. as
echo CHtml::encode($data->section_id);
I tried to bring it to work by having it as
$section=$data->section;
When I try the following code it displays the data
print_r($section);
But when I tried to get it as
echo $section->section_id;
or
echo $section['section_id'];
It gives error.
Any help?
Angelo
(Angelo)
December 24, 2010, 6:30am
6
PeRoChAk:
Any help?
Try checking if the value exists before you try and echo it. Because your are looping through results if one field in one row is missing you will get that error.
Try using
if(isset($section->section_id)) echo $section->section_id;
perochak
(Amjad Mughal)
December 24, 2010, 6:45am
7
Dear Angelo,
I know that these exists in the requested data.
Any help?
jacmoe
(Jacob Moen)
December 24, 2010, 7:22am
8
It’s obvious that it isn’t in the model.
What is the schema for your table?
perochak
(Amjad Mughal)
December 24, 2010, 9:20am
9
Dear,
there are three tables that are currently being accessed by this query
Sections with PK section_id
Categories with PK category_id
Section_category_relation which have the PKs of section and category tables.
The relations are as fellow
in Sections
categories -> array(self::has_many,categories,section_category_relation(section_id,category_id))
in Categories
sections -> array(self::has_many,‘sections’,section_category_relation(category_id,section_id))
and In section_category_relation
section -> array(self::belongs_to,‘sections’,section_id)
category ->array(self::belongs_to,‘categories’,‘category_id’)
So, by accessing the data of the categories, it will also fetch the data of the sections too. But it will not display it as should.
mdomba
(Maurizio Domba Cerin)
December 24, 2010, 9:57am
10
The relations HAS_MANY is a bit odd to me… you are creating them like they are MANY_MANY…
Check this thread, it can give you more ideas - http://www.yiiframework.com/forum/index.php?/topic/14390-search-for-many-many-value-in-cgridview/