簡體   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