Yii links error. Trying to get property of not object

I have a Cinema table and a City table and I have a relation to those tables by id .. and when I get echo results I have a PHP notification "Trying to get a non-object property"

What is the problem? or am I missing something?

My Code: Cinema 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(
        'countryCode' => array(self::BELONGS_TO, 'TblCountry', 'country_code'),
        'city' => array(self::BELONGS_TO, 'TblCity', 'city_id'),
        'tblCinemaMovies' => array(self::HAS_MANY, 'TblCinemaMovies', 'cinema_id'),
        'tblDays' => array(self::HAS_MANY, 'TblDay', 'cinema_id'),
        'tblShowtimes' => array(self::HAS_MANY, 'TblShowtime', 'cinema_id'),
    );
}

      

City 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(
        'tblCinemas' => array(self::HAS_MANY, 'TblCinema', 'city_id'),
        'countryCode' => array(self::BELONGS_TO, 'TblCountry', 'country_code'),
    );
}

      

View file:

<?php echo $model->city_id->name; ?>

      

+3


source to share


4 answers


<?php echo $model->city->name; ?>

      



You must use $model->city

to get the appropriate one city

$model

.

+4


source


I recommend that you always check if the relation returns null

or empty array

.

The following code works well with link types HAS_ONE

and BELONGS_TO

:

$cinema = Cinema::model()->find(); // get a cinema instance, assuming there is at least one row in the database.
$city = $cinema->city; // get the relation 
if ($city !== null) {
    // $city is a valid model
} else {
    // $city is null, the corresponding row does not exist in the database
}

      

You can also perform this check without using a new variable (in this case $city

):

if ($cinema->city!==null) {
    // the relation is available
}

      

Checking that a relationship is not returning null

is the best way to avoid PHP trying to get a property of a non-object. Also, if you are stuck with similar errors, it is recommended that you use features like, var_dump()

or better yet, a debugger.



Also note that the keys of the array in the array returned from the function relations()

are the property that needs to be accessed to get the relationship model:

public function relations() {
    return array(
         'city' => array( ... ),
         // city is the property that has to be accessed
         // Yii conventions recommend to use 'city_id' for the foreign key column name
    );
}

      

Also note that it is helpful to follow Yii conventions for naming relationships and columns to avoid using the same name for both the property and the relationship - in which case the relationship will not be available and probably an error, such as "trying a non-object access property "will appear when playing with a relation.

The latter, when working with relations HAS_MANY

or MANY_TO_MANY

, the relation returns an array of models, even if there is only one model and an empty array if nothing is available.

Explanation in the docs: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#performing-relational-query

+5


source


Since city_id is an array, in your viewfile you should write code like this

<?php echo $model->city_id[0]->name; ?>

      

+2


source


This error occurs when city_id is empty or null (or the value does not exist in the external table). If you are not sure if "city_id" exists, you can check it like this:

CHtml::value($model, 'city.name');

      

This way you always ensure that you don't have an exception if the value is empty or transparent

0


source







All Articles