Hi,
I’m creating my first Yii app and can understand how to use Foreign Keys.
I have 2 Tables :
Article
id
categoryId(fk)
title
dateCreated
Category
id
name
The models has the relations :
class Category extends CActiveRecord
{
public function relations()
{
return array(
'articles' => array(self::HAS_MANY, 'Article', 'categoryId'),
);
}
}
class Article extends CActiveRecord
{
public function relations()
{
return array(
'category' => array(self::BELONGS_TO, 'Category', 'categoryId'),
);
}
}
When i display an Article i would like to see the category name instead of its id.
I tried different things in the actionView of my ArticleController but couldnt do it.
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id)->with('category.articles')->find(),
));
}
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
'category'=>Category::model()->findByPk($this->loadModel($id)->categoryId),
));
}
But couldn’t reach it =s.
The view displays :
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'categoryId',
'title',
'description',
'content',
'image',
'url',
'tags',
'writer',
'dateCreated',
),
)); ?>
I found this link http://www.yiiframework.com/doc/guide/1.1/en/database.arr but still can’t do it :x.
I’m very new to Yii.
Thanks for your help.
Mex