dongzhuifeng1843 2014-04-13 06:47
浏览 40
已采纳

Yii - 如何在CGridView中显示用户自己上传的数据?

everyone I am working on a project that allow user do upload and download data. I want to show only a user's own uploaded data in CGridView. Here's my relation table enter image description here

I have tried to edit my page --> views/file/myUploads.php

<?php
$isMine=Yii::app()->user->id; // I have tried this
if($isMine){
    $this->widget('zii.widgets.CGridView',array(
    'id'=>'file-grid',
    'dataProvider'=>$model->search(),
    //'filter'=>$model,
    'columns'=>array(
        'name'
        'description',
        'category'
        'uploader'
        'upload_date',
        array(
            'header'=>'Actions',
            'class'=>'bootstrap.widgets.TbButtonColumn',
                'template'=>'{delete}', //'visible'=> (Yii::app()->user->getLevel()==1),
                'deleteConfirmation'=>"js: 'Are you want to delete '+$(this).parent().parent().children(':first-child').text()+ '?'",        
                'buttons'=>array(
                    'delete' => array(
                        'visible'=> '$data->pengunggah==Yii::app()->user->id',
                    ),
                )   
        ),
    ),
)); }
?>

I have tried that codes above but the page still display all data, not user's own data.

I have tried to edit myUpload function in FileController.php too

public function actionMyUpload()
{
    $isMine=Yii::app()->user->id; // I have tried this
    if($isMine){
        $model=new File('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['File']))
            $model->attributes=$_GET['File'];

        $this->render('myUpload',array(
        'model'=>$model,
        ));
    }
}

But still display all data.

Can anyone help me? Thanks a lot.

展开全部

  • 写回答

1条回答 默认 最新

  • douxian8883 2014-04-13 06:54
    关注

    Revert all your changes back, Open Your Model Class File ie "File.php" and add this line in your search() function:

    $criteria=new CDbCriteria;
    // Simply add this line 
    $criteria->addCondition('id_user='.Yii::app()->user->id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部