繁体   English   中英

yii中的模型关系

[英]relation in model in yii

我在模型中这样写了一个关系

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

我创建了一个带有子类别字段的视图页面,并通过数组对其进行了调用

我的观点

    <?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) 
            ),
    ),
)); ?>  

我的数据库中有一个名为sub_category的表,我需要做的就是与该表建立关系以获得subcategory_name ...
当我尝试它显示给我一个错误

试图获取非对象的属性

请有人帮我解决这个问题...
谢谢

首先检查类别和子类别字段是否不为空。

'value'=> ($model->subcategory) ? $model->subcategory->subcategory_name : null;
'value'=> ($model->category) ? $model->category->category_name : null;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM