dtkl55257 2015-08-20 11:55
浏览 44
已采纳

yii中的模型关系

I written a relation in my model somewhat like this

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(
        'category' => array(self::BELONGS_TO, 'Category', 'categoryid'),
        'company' => array(self::BELONGS_TO, 'CommercialUser', 'createdby'),
        'subcategory' => array(self::BELONGS_TO, 'SubCategory', 'subcategory'),
    );
}

and I created a view page with subcategory field and called it by array

my View

    <?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        //'id', 
        array( 
            'name' => 'type',
            'type' => 'raw',
            value'=>$model->type

            ),
        'title',
        'description',
        array( 
            'name' => 'Category',
            'type' => 'raw',
            'value'=>$model->category->categoryname 
            ),
         'subcategory',
         array( 
            'name' => 'subcategory',
            'type' => 'raw',
            'value'=>$model->subcategory->subcategory_name 
            ),
        'location',
        'startdate',
        'enddate', 



            array( 
            'name' => 'image',
            'type' => 'raw',
            'value'=>GFunctions::GetoffereveImage($model->id) 
            ),
    ),
)); ?>  

And I Have table called sub_category in my DB All i need is to make relation to that table to get the subcategory_name...
When I tried it is showing me an Error like

Trying to get property of non-object

Please somebody help me to fix this...
Thank you

  • 写回答

1条回答 默认 最新

  • duanhongqiong9460 2015-08-20 12:13
    关注

    First check if the category and subcategory fields is not null.

    'value'=> ($model->subcategory) ? $model->subcategory->subcategory_name : null;
    'value'=> ($model->category) ? $model->category->category_name : null;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同