Yii CRUD按数据库项目筛选项目的索引


Yii CRUD Index filtering items by db item

我刚刚为mysql数据库创建了一个Model和一个CRUD。

CREATE TABLE IF NOT EXISTS `issue` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `priority` varchar(255) DEFAULT NULL,
  `status` varchar(255) DEFAULT NULL,
  `user` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT;

一切都很好,但人们可以在index.php上看到彼此的列表吗?r=问题/索引。我不希望这种情况发生,我想过滤索引列表,按用户搜索所有帖子,只显示他的帖子。

我该怎么做?

第1版:

这是视图(_V),列表显示给所有人:

<?php
/* @var $this IssueController */
/* @var $data Issue */
?>
<div class="view">
<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('title')); ?>:</b>
<?php echo CHtml::encode($data->title); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('description')); ?>:</b>
<?php echo CHtml::encode($data->description); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('priority')); ?>:</b>
<?php echo CHtml::encode($data->priority); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('status')); ?>:</b>
<?php echo CHtml::encode($data->status); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('user')); ?>:</b>
<?php echo CHtml::encode($data->user); ?>
<br />

</div>

第2版:

我插入了下面的代码,但当我登录到一个有帖子的有效用户时,我得到了一个错误:

<b><?php echo CHtml::encode($data->findAll('user LIKE '''.Yii::app()->user->name.'''', array('title'=>$data->title))); ?>:</b>
<?php echo CHtml::encode($data->findAll('user ='.Yii::app()->user->name, array('title'=>$data->title))); ?>
<br />

错误:

htmlspecialchars() expects parameter 1 to be string, array given

第3版:

我刚刚意识到我的逻辑是错误的,如果用户有一个以上的帖子,这将不会做到,我需要使用foreach来提取值并回显它们,但仍然不确定如何做到这一点。

EDIT4解决了问题

这是我的新视图(_V):

<?php $foundAll=$data->findAllByAttributes(array('title'=>$data->title, 'description'=>$data->description, 'priority'=>$data->priority, 'status'=>$data->status, 'user'=>Yii::app()->user->name));//,'user LIKE '.''''.Yii::app()->user->name.'''');
//then you need use like
foreach($foundAll As $found)
{
    echo "<b>".$data->getAttributeLabel('id')."</b>: ";
    echo CHtml::link(CHtml::encode($found['id']), array('view', 'id'=>$found['id']));
    echo "<br />";
    //here you need to use array, like this $found['userId'].
    echo "<b>".$data->getAttributeLabel('title')."</b>: ";
    echo "".$found['title']."";
    echo "<br />";
    echo "<b>".$data->getAttributeLabel('description')."</b>: ";
    echo "".$found['description']."";
    echo "<br />";
    echo "<b>".$data->getAttributeLabel('priority')."</b>: ";
    echo "".$found['priority']."";
    echo "<br />";
    echo "<b>".$data->getAttributeLabel('status')."</b>: ";
    echo "".$found['status']."";
    echo "<br />";
}
?>

您必须检查控制器(IssueController)或模型(Issue),并找到变量$dataProvider的定义。它看起来像这样:

$dataProvider=new CActiveDataProvider('Issue');

只需将其更改为:

$criteria = new CDbCriteria();
$criteria->addSearchCondition('user', Yii::app()->user->name);
$dataProvider=new CActiveDataProvider('Issue', 'criteria'=>$criteria);