Php Yii与MVC模式
我正在用Yii创建一个新的Portlet。此小部件显示问题的最新评论。我只想在问题有评论时显示此内容,如果问题没有任何评论,则不显示(事件标题) 因此,我的psudo代码在视图文件中如下所示: 检查流程中的评论数量:Php Yii与MVC模式,php,model-view-controller,yii,Php,Model View Controller,Yii,我正在用Yii创建一个新的Portlet。此小部件显示问题的最新评论。我只想在问题有评论时显示此内容,如果问题没有任何评论,则不显示(事件标题) 因此,我的psudo代码在视图文件中如下所示: 检查流程中的评论数量: <?php $countIssue = count($model->issues); $i = 0; $j = 0; while($i < $countIssue) { $j += $model->issues[$i]->commentCoun
<?php
$countIssue = count($model->issues);
$i = 0; $j = 0;
while($i < $countIssue)
{
$j += $model->issues[$i]->commentCount;
$i ++;
}
?>
if ($countIssue >0 ) {
if ($j >0)
Display the widget
}
Else
Don't display the widget
如果($countIssue>0){
如果($j>0)
显示小部件
}
其他的
不显示小部件
我只是想知道我的代码是否适合MVC模式。你能给我一个方向吗?我应该将注释过程的检查编号带到模型或控制器,还是上面的MVC模式可以
谢谢大家! 首先,我会将此逻辑移到portlet类(扩展CPorlet的类)的run()方法中 接下来,我将定义一个in-Issue类。此关系仅用于计算注释,并允许您使用以下语句:
$issue = Issue::model()->findByPk($issue_id);
// $comments_count below is exactly what you would expect... .
$comments_count = $issue->commentsCount;
最后,结合这一切,我建议在portlet的run()方法中采用以下方法:
If ($someIssue->commentsCount > 0) {
// do something and in the end, when you want to render the portlet, you do...
$this->render("view_file_name");
}
我认为有很多MVC友好的方法可以做到这一点,主要思想是将数据逻辑放入模型,然后通过控制器处理请求,而理想情况下视图应仅用于显示目的 就我个人而言,我将使用Yii(最初来自Rails)来实现如下最新过滤器: 型号:
class Comment extends CActiveRecord
{
......
public function scopes()
{
return array(
'recently'=>array(
'order'=>'create_time DESC',
'limit'=>5,
),
);
}
}
如果只有问题有一些注释,要获取列表注释,可以在控制器中执行类似操作:
if($issue->comments)
$isseComments = Comment::model()->recently()->findAll(); //using the named scope to the recent ones only
$this->render('your_action',array(
.....
'issue' => $issue,
'issueComments'=>$isseComments,
));
您的视图将保持整洁:
if($issueComments > 0)
//Display the widget
else
// Don't