我刚刚为 mysql 数据库创建了一个模型和一个 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=issue/index.php上看到彼此的列表。我不希望这种情况发生,我想过滤索引列表以搜索用户的所有帖子并只显示他的帖子。
我怎样才能做到这一点?
编辑1:
这是_view,向所有人显示列表:
<?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:
我刚刚意识到我的逻辑是错误的,如果用户有超过 1 个帖子,这是不行的,我需要使用 foreach 来提取值并回显它们,但仍然不确定如何做到这一点。
编辑4解决了它
这是我的新_view:
<?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 />";
}
?>
最佳答案
您必须检查 Controller (IssueController)或模型(Issue)并找到变量$dataProvider的定义。 它看起来像这样:
$dataProvider=new CActiveDataProvider('Issue');
只需将其更改为:
$criteria = new CDbCriteria();
$criteria->addSearchCondition('user', Yii::app()->user->name);
$dataProvider=new CActiveDataProvider('Issue', 'criteria'=>$criteria);
关于php - Yii CRUD 索引按数据库项过滤项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20443689/