有事没事来多放点。多放点就越来越专业。

第七天、模型与视图操作

上一篇 / 下一篇  2008-02-14 18:25:04

       

最新问题列表

增加最新问题列表页面实际上就是增加 question 模块的 recent 方法。 需要做的工作包括:(这也是 smyfony 增加页面的基本步骤)

  • 增加 executeRecent 方法(apps/frontend/modules/question/actions/actions.class/php 之中)
  • 增加 recentSuccess.php 模版(apps/frontend/modules/question/templates 之中)
  • 增加相应的路由规则 recent_questions (在apps/frontend/config/routing 文件)

增加 executeRecent 方法

增加 executeRecent 方法(apps/frontend/modules/question/actions/actions.class/php 之中)

publicfunctionexecuteRecent()
{
$this->question_pager= QuestionPeer::getRecentPager($this->getRequestParameter('page',1));dddddddddddddddddddddddddddddddddddddddddddddddddddddd
}

但是 QuestionPeer 的 getRecentPager 方法并不存在,需要在 lib/model/QuestionPeer.php 中创建:

QuestionPeer 中创建 getRecentPager 方法

publicstaticfunctiongetRecentPager($page)
{
$pager=newsfPropelPager('Question',sfConfig::get('app_pager_homepage_max'));
$c=newCriteria();
$c->addAscendingOrderByColumn(self::CREATED_AT);
$pager->setCriteria($c);
$pager->setPage($page);
$pager->setPeerMethod('doSelectJoinUser');//将问题的相关用户信息一起从数据库中调出
$pager->init();
 
return$pager;
}

增加 recentSuccess.php 模版

增加 recentSuccess.php 模版 (apps/frontend/modules/question/templates 之中)

<h1>recent questions</h1>
<?phpinclude_partial('question_list',array('question_pager'=>$question_pager))?>

增加相应的路由规则 recent_questions

增加路由规则 recent_questions (在apps/frontend/config/routing 文件)

recent_questions:
url: /recent/:page
param: { module: question, action: recent, page: 1 }
page: 1 是设置默认值,即 访问 /recent 而不提供 page 值时使用 1 作为 page 值。

显示效果

最新答案列表

最新答案列表需要如下工作来建立:

  • 建立 answer 模块
  • recent 页面 (行为及行为用到的Peer方法、模版)

建立 answer 模块

symfony-init-module frontend answer

建立 answer 页面

建立 answer recent 行为

answer recent 行为

publicfunctionexecuteRecent()
{
$this->answer_pager= AnswerPeer::getRecentPager($this->getRequestParameter('page',1));
}

AnswerPeer::getRecentPager 方法

getRecentPager 方法

publicstaticfunctiongetRecentPager($page)
{
$pager=newsfPropelPager('Answer',sfConfig::get('app_pager_homepage_max'));
$c=newCriteria();
$c->addAscendingOrderByColumn(AnswerPeer::CREATED_AT);
$pager->setCriteria($c);
$pager->setPage($page);
$pager->setPeerMethod('doSelectJoinUser');
$pager->init();

return$pager;
}

建立 answer recentSuccess.php 模版

answer/recent 模版

<?phpuse_helper('Date')?>
 
<h1>Recent Answers</h1>
<?phpinclude_partial('answer_list',array('answer_pager'=>$answer_pager))?>
 
<div id="answer_pager">
<?phpif($answer_pager->haveToPaginate()):?>
<?phpecholink_to('&laquo;','answer/recent?page=1')?>
<?phpecholink_to('&lt;','answer/recent?page='.$answer_pager->getPreviousPage())?>

<?phpforeach($answer_pager->getLinks()as$page):?>
<?phpecholink_to_unless($page==$answer_pager->getPage(),$page,'answer/recent?page='.$page)?>
<?phpecho($page!=$answer_pager->getCurrentMaxLink())?'-':''?>
<?phpendforeach;?>
<?phpecholink_to('&gt;','answer/recent?page='.$answer_pager->getNextPage())?>
<?phpecholink_to('&raquo;','answer/recent?page='.$answer_pager->getLastPage())?>
<?phpendif;?>
</div>
_answer_list.php 与 _question_list.php 相似,不必赘述

显示效果

问题的兴趣者列表

这里要显示的是对问题感兴趣的用户,可以将这个页面放在问题模块或者用户模块中。 姑且放在用户模块之中。 添加页面的步骤没有变还是:添加行为和模版。

添加行为(action)

apps/frontend/modules/user/actions/actions.php 中添加

publicfunctionexecuteListInterestedBy()
{
$this->question= QuestionPeer::retrieveByPk($this->getRequestParameter('id'));
$this->forward404Unless($this->questioninstanceof Question);

$this->interested_users=$this->question->getAllInterestedUsers();
}

由于 getAllInterestedUsers() 是一个需要编写的类,需要在 Question.php 中添加:

lib/model/Question.php 中添加

publicfunctiongetAllInterestedUsers()
{
$c=newCriteria();
$c->addJoin(UserPeer::ID, InterestPeer::USER_ID, Criteria::LEFT_JOIN);
$c->add(InterestPeer::QUESTION_ID,$this->getId());
returnUserPeer::doSelect($c);
}

添加模版( templates )

list

<?php use_helper('Date') ?>
 
<h1><?php echo $question->getTitle() ?></h1>

<p>asked by <strong><?php echo $question->getUser() ?></strong> <?php echo time_ago_in_words($question->getCreatedAt('U')) ?></p>

<?php echo $question->getInterestedUsers() ?> askeet users are interested by this question

<ul>
<?php foreach ( $interested_users as $interested_user ): ?>
<li><?php echo link_to($interested_user->__toString(),'user/show?id='.$interested_user->getId()) ?></li>
<?php endforeach; ?>
</ul>

显示效果

/user/listInterestedBy/id/XX

用户资料

添加行为

user 模块的 show 行为 executeShow()方法

publicfunctionexecuteShow()
{
$this->subscriber= UserPeer::RetrieveByPk($this->getRequestParameter('id',$this->getUser()->getSubscriberId()));
$this->forward404Unless($this->subscriber);
 
$this->interests=$this->subscriber->getInterestsJoinQuestion();
$this->answers=$this->subscriber->getAnswersJoinQuestion();
$this->questions=$this->subscriber->getQuestions();

}

添加模版

user 模块的 showSuccess.php

<?phpecho$subscriber?>
 
<h2>Interests</h2>


<ul>
<?phpforeach($interestsas$interest):$question=$interest->getQuestion()?>
<li><?phpecholink_to($question->getTitle(),'question/show?stripped_title='.$question->getStrippedTitle())?></li>
<?phpendforeach;?>
</ul>


<h2>Contributions</h2>


<ul>
<?phpforeach($answersas$answer):$question=$answer->getQuestion()?>
<li>
<?phpecholink_to($question->getTitle(),'question/show?stripped_title='.$question->getStrippedTitle())?><br />
<?phpecho$answer->getBody()?>
</li>
<?phpendforeach;?>
</ul>


<h2>Questions</h2>


<ul>
<?phpforeach($questionsas$question):?>
<li><?phpecholink_to($question->getTitle(),'question/show?stripped_title='.$question->getStrippedTitle())?></li>
<?phpendforeach;?>
</ul>

显示效果

/user/show/id/XX

增加导航条

布局中添加组建

定义组建中包含的行为

将布局模版 layout.php 中的页面中

<!-- Nothing for the Moment -->

修改为导航条:

组建引用

<?phpinclude_component_slot('sidebar')?>

将组建与行为关联

在应用级 view.yml components 部分

default:
components:
sidebar: [sidebar, default]

编写 sidebar/default 行为和模版

idebar/default 行为

是在 components.class.php ,而不是在 actions.class.php 中

idebar/default 行为

 

idebar/default 模版

idebar/default 模版

<?phpecholink_to('ask a new question','question/add')?>
 
<ul>
<li><?phpecholink_to('popular questions','question/list')?></li>
<li><?phpecholink_to('latest questions','question/recent')?></li>
<li><?phpecholink_to('latest answers','answer/recent')?></li>
</ul>

限制直接访问

在 side 模块建立 config/module.yml 文件,限制直接访问

all:
is_internal: on

一点视图设置

根据情况修改view.yml

修改meta

metas:
title: askeet! ask question, find answers
robots: index, follow
descrīption: askeet! a symfony project built in 24 hours
keywords: symfony, project, askeet, php5, question, answer
language: en

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

数据统计

  • 访问量: 44134
  • 日志数: 283
  • 图片数: 1
  • 建立时间: 2007-09-07
  • 更新时间: 2008-04-16

RSS订阅

Open Toolbar