Yii CRUD索引按db项过滤项目

时间:2013-12-07 16:28:41

标签: php html mysql yii

我刚为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 = issue / index。我不希望这种情况发生,我想过滤索引列表以按用户搜索所有帖子,只显示他的帖子。

我该怎么做?

EDIT1:

这是_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>

EDIT2:

我插入了下面的代码,但是当我登录有帖子的有效用户时出现错误:

<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

EDIT3:

我刚刚意识到我的逻辑是错误的,如果用户有超过1个帖子就不行,我需要使用foreach来提取值并回显它们,但仍然不确定如何去做。

EDIT4已解决此问题

这是我的新_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 />";
}
?>

1 个答案:

答案 0 :(得分:0)

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

$dataProvider=new CActiveDataProvider('Issue');

只需将其更改为:

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