Reputation: 1785
I have created one model for Event Categories, and another model for Events. When user will select any particular event category, I want to display all the events under that category.
The following are the relations I've given to their models. EventCategory.php Model:
return array(
'event' => array(self::HAS_MANY, 'Event', 'event_category_id'),
);
Event.php Model:
return array(
'category' => array(self::BELONGS_TO, 'EventCategory', 'event_category_id'),
);
When I try to return events in event category view, NOT SET is returned.
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'evntcatm_id',
'evntcatm_name',
'evntcatm_desc',
'evntcatm_img',
'event.evntm_evntcatm_id',
'event.evntm_name',
),
)); ?>
Where I am going wrong?
Upvotes: 0
Views: 319
Reputation: 11473
It looks as if you are trying to return a list of events with the event category information populated. Instead of going at it from the event category, I would go at it from the event. Then you can access the event category information with a shortcut. Something like this:
In the controller
$model = Event::model()->fetchAll();
Then in the view your attributes array would look like
'attributes' => array(
'category.evntcatm_id',
'category.evntcatm_name',
...
'evntm_evntcatm_id',
'evntm_name',
),
Upvotes: 0
Reputation: 1785
I have used loadModel to fetch the Category ID and then this pass ID to dataProvider to compare the data from database and returned the results.
EventCategoryController.php
public function actionView($id)
{
$this->layout = 'main';
$model = $this->loadModel($id);
$criteria = new CDbCriteria;
$criteria->compare('evntm_sts','a',true);
$criteria->compare('evntm_evntcatm_id',$model->evntcatm_id,true);
$dataProvider = new CActiveDataProvider('Event', array(
'criteria'=>$criteria,
));
$this->render('view',array(
'model'=>$model,
'dataProvider'=>$dataProvider,
));
}
In View:
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_event',
'summaryText'=> '',
)); ?>
Upvotes: 0
Reputation: 8587
The relation event
int your category is a HAS_MANY
. So $category->event
will be an array of Event
records. So how should Yii know, what to do with that list of objects? You could add getters to your Category
model, though. For example for the event names:
public function getEventNames()
{
$names = array();
foreach($this->events as $event) {
$names[] = $event->eventm_name;
}
return implode(', ', $names);
}
And then use eventNames
in your detailview as if it where a regular category attribute.
Upvotes: 1