I've these tables

estudiantes

id_estudiante pk nombre_estudiante

evaluaciones

id_evaluacion pk evaluacion_estudiante evaluacion_asignatura

asignaturas

id_asignatura pk nombre_asignatura

in evaluaciones model relations

return array(
                'estudiantes'=>array(self::BELONGS_TO, 'Estudiantes', 'evaluacion_estudiante'),
        );

evaluaciones's _view file i have this

<?php echo CHtml::encode($data->estudiantes->nombre_estudiante); ?>

and there is an error on that line, it seems to be a problem with relations.. but i cant solve it.

Trying to get property of non-object

有帮助吗?

解决方案

This error occurs when you are trying to echo something that doesn't exist.

The best way to get rid of this error is to check your value first before rendering it for output.

you can do:

if(!empty($data->estudiantes->nombre_estudiante))
<?php echo CHtml::encode($data->estudiantes->nombre_estudiante); ?>

or using ternary:

<?php (!empty($data->estudiantes->nombre_estudiante)?
echo CHtml::encode($data->estudiantes->nombre_estudiante) : "null value"; ?>

其他提示

This happens if there's a row in table "evaluaciones" with null value in the field evaluacion_estudiante.

If so, you must check if $data->estudiantes is null before echo.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top