简体   繁体   中英

Yii - How To Display User's Own Uploaded Data In 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 在此处输入图片说明

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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