Good afternoon
I’m studying about relations, I get it how to create a relation, but I don’t know How to view the results.
For example, I have two tables: produto and usuarios. The table produto has a column usuario_id and a foreign key "usuario_id" that have a relation with table usuarios column id, This is the sql that I create this:
alter table produto add CONSTRAINT `id_usuario_fk` FOREIGN KEY (`id_usuario`) REFERENCES `usuarios` (`id`)
In my class model Produto I have this relation:
public function relations()
{
return array(
'usuario' => array(self::BELONGS_TO, 'Usuarios', 'id_usuario'),
);
}
In my class Usuarios I have this relation:
public function relations()
{
return array(
'produtos' => array(self::HAS_MANY, 'Produto', 'id_usuario'),
);
}
In my ProdutoController I have this loadModel and actionView:
public function actionView()
{
$this->render('view',array(
'model'=>$this->loadModel(),
));
}
public function loadModel()
{
if($this->_model===null)
{
if(isset($_GET['id']))
// $this->_model=Produto::model()->with(array('usuario'=>array('with'=>'produtos')))->findbyPk($_GET['id']);
$this->_model=Produto::model()->with('usuario')->findbyPk($_GET['id']);
//$this->_model=Produto::model()->findbyPk($_GET['id']);
if($this->_model===null)
throw new CHttpException(404,'The requested page does not exist.');
}
return $this->_model;
}
this is my widgets of view.php about produtos:
$this->breadcrumbs=array(
'Produtos'=>array('index'),
$model->id,
);
....
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'valor',
'codigo_barras',
'data',
'id_usuario',
),
));
So on my table usuarios there is the column “nome” and I want show this column on view.php of produtos, I did the relations but I don’t know how show this column that belong to usuarios. How can I do this?
The fields about table produtos show ok, I need only show the field "nome" of usuarios on view.php of produtos