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 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊