[SOLVED??]Relations with fields optionals

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));

	}




If you want to render the table in a subform (using renderPartial), remember to propagate $model to the subform




$this->renderPartial('some_form', array('model'=>$model));



/Tommy

Sorry

I do not understand the answer in context with the question

my problem is with field optionals and relations

Please be patient, my english is poor

Sorry, I misunderstood your problem.

One thought: $model might not be assigned to here thus can’t be validated in save().




$model->attributes=$_POST['Rp_matriculas'];



/Tommy

the error is in render




<?php echo CHtml::activeTextField($model->especialidad,'descripcion',array('size'=>40,'readonly'=>'true')); ?>



the field is optional and $model->especialidad->descripcion

do not exists

why when show in view admin or list work fine

but in update not?

Just a guess:

$model->especialidad isn’t an object because the fk esp_idEspecialidad in table matricula can be a null value.

/Tommy

Yes, I know

but hoped that the framework is resolved

my solution …

add a attribute to model




   public $descripcion_especialidad;



in the 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.');

                $this->_model->descripcion_especialidad = $this->_model->especialidad->descripcion;

                }

                return $this->_model;

        }




view




<?php echo CHtml::activeTextField($model,'descripcion_especialidad',array('size'=>40,'readonly'=>'true')); ?>



Greetings and thanks

Other more simple solution




    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.');

                        if ($this->_model->especialidad==null){

                            $this->_model->especialidad=new Rp_especialidades;

                        }

        }

        return $this->_model;

    }




I’ve found CHtml::value() in the API doc.

http://www.yiiframework.com/doc/api/CHtml#value-detail

Might be an alternative for optional fields (not tested).

/Tommy