【问题标题】:Yii Relations error Trying to get property of non-objectYii 关系错误试图获取非对象的属性
【发布时间】:2012-03-25 01:28:47
【问题描述】:

我有 Cinema 表和 City 表,我通过 id 与这个表有关系。当我回显结果时,我有 PHP 通知“试图获取非对象的属性”

有什么问题?还是我错过了什么??

我的代码: 电影模型

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'),
    );
}

城市模型

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'),
    );
}

查看文件:

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

【问题讨论】:

  • 您在哪里创建modelcity_id?你确定它们存在吗?
  • 不,我没有我认为 $model->city_id->name;是 fase 但这是工作 $model->city_id;结果 = 身份证号码.. :( 我怎样才能得到“名字”?

标签: php mysql yii


【解决方案1】:

我建议您始终检查关系是否不返回 nullempty array

以下代码适用于HAS_ONEBELONGS_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
}

您也可以在不使用新变量的情况下进行此检查(在本例中为$city):

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

检查关系是否不返回 null 是避免 PHP 错误“试图获取非对象的属性”的最佳方法。此外,如果您遇到类似的错误,建议使用 var_dump() 之类的函数,或者更好的是使用调试器。

另外,请注意relations() 函数返回的数组中的数组键是获取关系模型必须访问的属性:

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

还要注意,最好遵循 Yii 命名关系和列的约定,以避免对属性和关系使用相同的名称 - 在这种情况下,关系将不可用,并且可能会出现类似“尝试访问玩关系时会弹出非对象的属性。

最后一件事,当使用HAS_MANYMANY_TO_MANY 关系时,即使只有一个模型可用,关系也会返回一个模型数组,如果没有可用模型,则返回一个空数组。

文档中的解释: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#performing-relational-query

【讨论】:

    【解决方案2】:
    <?php echo $model->city->name; ?>
    

    你必须使用$model-&gt;city来获取$model对应的city

    【讨论】:

      【解决方案3】:

      因为city_id是一个数组,所以在你的视图文件中,你应该这样写代码

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

      【讨论】:

        【解决方案4】:

        当 city_id 被清空或为空(或外部表中不存在该值)时会发生该错误。 如果您不确定“city_id”是否存在,可以这样检查:

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

        这样,您始终可以确保在值为空或清除时不会出现异常

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-23
          • 1970-01-01
          相关资源
          最近更新 更多