Hi
my problem:
two tables with a optional relation
table matricula field idEspecialidad optional
table especialidades (idEspecialidad,descripcion)
when show data (not in a form), all work fine.
<td><?php echo CHtml::encode($model->especialidad->descripcion); ?></td>
but if show data for update in a form, the next error display
Fatal error: Call to a member function hasErrors() on a non-object in /opt/lampp/htdocs/sistemas/yii-1.0.9.r1396/framework/web/helpers/CHtml.php on line 1589
try put in the form
<td><?php echo CHtml::encode($model->especialidad->descripcion); ?></td>
and the error appears again
model
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'profesional' => array(self::BELONGS_TO, 'Rp_profesionales', 'pro_idProfesional','alias'=>'Rp_profesionales'),
'titulo' => array(self::BELONGS_TO, 'Rp_titulos', 'tit_idTitulo','alias'=>'Rp_titulos'),
'especialidad' => array(self::BELONGS_TO, 'Rp_especialidades', 'esp_idEspecialidad','alias'=>'Rp_especialidades'),
'institucion' => array(self::BELONGS_TO, 'Rp_instituciones', 'ins_idInstitucion','alias'=>'Rp_instituciones'),
'novedades' => array(self::HAS_MANY, 'Rp_novedades', 'mat_idMatricula'),
);
}
view
<div class="simple">
<?php echo CHtml::activeHiddenField($model,'esp_idEspecialidad'); ?>
<?php echo CHtml::activeLabelEx($model,'esp_idEspecialidad'); ?>
<?php echo CHtml::activeTextField($model->especialidad,'descripcion',array('size'=>40,'readonly'=>'true')); ?>
<?php echo CHtml::link(CHtml::image('images/aflist.gif','Lov'), '#', array('onclick'=>'lovEspecialidades()')); ?>
<?php echo CHtml::image('images/delete.png','Limpia', array('onclick'=>'limpia("Rp_matriculas_esp_idEspecialidad","Rp_especialidades_descripcion")')) ;?>
</div>
controler
public function loadRp_matriculas($id=null)
{
if($this->_model===null)
{
if($id!==null || isset($_GET['id']))
$this->_model=Rp_matriculas::model()->with('profesional.persona','titulo','especialidad')->findbyPk($id!==null ? $id : $_GET['id']);
if($this->_model===null)
throw new CHttpException(404,'The requested page does not exist.');
}
return $this->_model;
}
public function actionUpdate()
{
$model=$this->loadRp_matriculas();
if(isset($_POST['Rp_matriculas']))
{
$model->attributes=$_POST['Rp_matriculas'];
if($model->save())
$this->redirect(array('show','id'=>$model->idMatricula));
}
$this->render('update',array('model'=>$model));
}
public function actionCreate()
{
$model=new Rp_matriculas;
if(isset($_POST['Rp_matriculas']))
{
$model->attributes=$_POST['Rp_matriculas'];
if($model->save())
$this->redirect(array('show','id'=>$model->idMatricula));
}
$this->render('create',array('model'=>$model));
}