简体   繁体   English

如何在Yii中使用CListView自定义dataProvider

[英]How to customize dataProvider using CListView in Yii

I'm trying to output data into the CListView of the current user only. 我正在尝试仅将数据输出到当前用户的CListView中。 So far, if I put in the $dataProvider, it only outputs ALL the records from the database. 到目前为止,如果我放入$ dataProvider,它将仅输出数据库中的所有记录。

This is my current code: 这是我当前的代码:

    $current = Yii::app()->user->id;
    $currentid = Yii::app()->db->createCommand("select * from content where id = ". $current)->queryRow();

    $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider, //This is the original. I tried replacing it
                                   //with $currentid but errors.
    'itemView'=>'_view2',
    'template'=>'{items}<div>{pager}</div>',
    'ajaxUpdate'=>false,
    )); 

From what I understand from the Yii Documentations, $dataProvider stores all the data within the database and places it inside the dataProvider itself and my "_view2" uses that to output all the records. 据我从Yii文档中了解到,$ dataProvider将所有数据存储在数据库中,并将其放置在dataProvider本身中,而我的“ _view2”使用它来输出所有记录。

My Controller codes for the showing/view is as follows: 我的控制器用于显示/查看的代码如下:

 public function actionView()
{

    $post=$this->loadModel();
    if(Persons::model()->compare_country(explode("|",$post->country)))
    {
        $post->view_count = $post->view_count + 1;
        Yii::app()->db->createCommand("UPDATE content SET view_count = {$post->view_count} WHERE id = {$post->id}")->execute();
        //$post->save();
        $comment=$this->newComment($post, 'view');

        if (!empty(Yii::app()->session['announcement_message']))
        {
            Yii::app()->user->setFlash('message',Yii::app()->session['announcement_message']);
            Yii::app()->session['announcement_message'] = null;
        }

        $this->render('view',array(
            'model'=>$post,
            'comment'=>$comment,
            'view'=>'view',
        ));
    }
    else
    {
        $this->redirect(Yii::app()->createAbsoluteUrl('news/index',array('page'=>'1')));
    }
}

public function actionShow($id)
{
    $post=$this->loadModel($id);
    $comment=$this->newComment($post);
        $attachments=Attachments::model()->findAllByAttributes(array(
                'content_id' => $id,
                ));
    $this->render('show',array(
        'model'=>$post,
        'comment'=>$comment,
            'attachments'=>$attachments
    ));
}

If you wanted to see my _view2, these are my codes: 如果您想查看我的_view2,这些是我的代码:

 <div class="profile-member-post-box announcement" >
<div class="events-post-bodytext profile-member-info">

    <?php $person=Persons::model()->findByAttributes(array('party_id'=>$data->party_id)); 

    if ($person->party_id === Yii::app()->user->id)
        {
    ?>
            <span><?=CHtml::link($data->title, array('view', 'id'=>$data->id), array('class' => 'titlelink'));?></span>
    <?php 
        $country=Lookup_codes::model()->findByAttributes(array('id'=>$person->country)); 
        $location = empty($country) ? '' : 'of '.$country->name;
        $sysUser=User::model()->findByAttributes(array('party_id'=>$data->party_id));

    ?>
    <p>
        By: <?php echo CHtml::link($person->getusername(), array('persons/view/id/'.$person->showViewLinkId())); ?>
        <span class="date2"> - <?php echo date('M j, Y',strtotime($data->date_created)); ?></span>
    </p>

            <div>
            <?php if(Yii::app()->partyroles->isAdmin() || ((get_access('Announcement','edit') && (Yii::app()->user->id == $data->party_id)) || (get_local_access('sub-admin','edit',$data->id)))):?>
                <a href="<?php echo Yii::app()->createUrl('announcement/update', array('id'=>$data["id"]))?>">Edit</a> | <?php endif;?> <?php echo (Yii::app()->partyroles->isAdmin() || (get_access('Announcement','delete') && (Yii::app()->user->id == $data->party_id)) || (get_local_access('sub-admin','delete',$data->id))) ? CHtml::link('Delete','#',array('submit'=>array('delete','id'=>$data["id"]),'confirm'=>'Are you sure you want to delete this item?')) : NULL?>
            </div>

    <?php
    }
    else
    ?>

</div>

I just need to be able to fix the view to show records only by the current user. 我只需要能够修复视图以仅显示当前用户的记录。

UPDATE!!------------ UPDATE!------------

I'm going to add my actionIndex here: 我将在此处添加我的actionIndex:

 public function actionIndex()
{
    if(get_access('Announcement','view') || get_access('Announcement','view_local'))
    {
    $id = Yii::app()->user->id;
    $condition = Persons::model()->get_view_condition('Announcement');
    $criteria=new CDbCriteria(array(
        'condition'=>'1=1 '.$condition,
        'order'=>'date_modified DESC',
        'with'=>'commentCount',
    ));
    /*
    if(isset($_GET['tag']))
        $criteria->addSearchCondition('tags',$_GET['tag']);
 */

    $items=SystemParameters::model()->findAllByAttributes(array(
                'name' => 'blogs_per_page',
                ));

    $dataProvider=new CActiveDataProvider('Announcement', array(
        'pagination'=>array(
            'pageSize'=>strip_tags($items[0]->value),
        ),
        'criteria'=>$criteria,
    ));

   /* $dataProvider=new CActiveDataProvider('Announcement', array(
        'pagination'=>array(
            'pageSize'=>5,
        ),
        'criteria'=>$criteria,
    ));*/

    //$dataProvider=Announcement::model()->findAll();

    $attachments=Attachments::model()->findAllByAttributes(array(
                'content_id' => $id,
                ));

    if (!empty(Yii::app()->session['announcement_message']))
    {
        Yii::app()->user->setFlash('message',Yii::app()->session['announcement_message']);
        Yii::app()->session['announcement_message'] = null;
    }

    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
    }
else
{
    $this->redirect(Yii::app()->createAbsoluteUrl('news/index',array('page'=>'1')));
}
}

Your question is very hard to follow... but I'll attempt to answer by giving an example of how to use the CDataProvider and CListView to display all of the Announcements owned by the current logged in User. 您的问题很难回答...但是我将尝试通过示例说明如何使用CDataProvider和CListView来显示当前登录用户所拥有的所有公告。 This assumes the Announcement model's table has a user_id field which contains the id of the User who owns or created it. 假定“公告”模型的表具有一个user_id字段,其中包含拥有或创建它的用户的ID。

First, in your indexAction() in your controller: 首先,在控制器的indexAction()中:

// get the logged in user's ID
$userId = Yii::app()->user->id;
// now define the dataprovider, which will do the SQL query for you
$dataProvider = new CActiveDataProvider( // declare a new dataprovider
  'Announcement', // declare the type of Model you want to query and display
  array( // here we build the SQL 'where' clause
    'criteria' => array( // this is just building a CDbCriteria object
      'condition' => 'user_id=:id', // look for content with the user_id we pass in
      'params' => array(':id' => $userId), // pass in (bind) user's id to the query
      //'order'=>'date_modified DESC', // add your sort order if you want?
      //'with'=>'commentCount', // join in your commentCount table?
    )
  )
);
$this->render('index',array( // render the Index view
  'dataProvider'=>$dataProvider, // pass in the data provider
));

Then in your index.php view: 然后在index.php视图中:

// create the CListView and pass in the $dataProvider we created above, in the indexAction
$this->widget('zii.widgets.CListView', array(
  'dataProvider'=>$dataProvider, // this is the data provider we just created
  'itemView'=>'_view2',
  'template'=>'{items}<div>{pager}</div>',
  'ajaxUpdate'=>false,
));

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

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