openPNE常用方法分享
更新時間:2011年11月29日 22:47:05 作者:
openPNE常用方法分享,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
<?php include_partial('sidemenu',array('form'=>'asdfgasgsad'));?>這句話意思是包含'_sidemenu.php'并往其頁面?zhèn)饕幌盗袇?shù),'_sidemenu.php'頁即可直接使用$form變量中的值
<?php
op_include_box('vote_question_create_box','<strong>asdfasdf</strong>',array('title'=>'創(chuàng)建問題','moreInfo'=>array('創(chuàng)建問題',link_to('創(chuàng)建問題2','@my_index'))));
?>
<?php
op_include_box('vote_question_create_box',get_slot('pager'),array('title'=>'創(chuàng)建問題','moreInfo'=>array('創(chuàng)建問題',link_to('創(chuàng)建問題2','@my_index'))));
?>
'vote_question_create_box'只是一個標記,'<strong>asdfasdf</strong>'或 get_slot('pager')則是要輸出到頁面上標題下的信息(這個方法里要包含slot只能用get_slot()不能用include_slot(),
而在頁面中要包含slot則必須使用include_slot())
第三個數(shù)組參數(shù)中的鍵值名稱title是固定的,是該段'vote_question_create_box'顯示的標題,后面的'moreInfo'鍵名也是固定鍵值對應(yīng)的數(shù)組則是羅列顯示的內(nèi)容列表
<?php slot('pager'); ?>設(shè)定一個slot段落
<?php echo 'asdfasgsadfasdfaaaaaaaaaaaaaaaaaaaaaa' ?>
<?php end_slot() ?>
<?php include_slot('pager'); ?>包含指定的slot段落,設(shè)定的slot段落必須通過包含才能在頁面上顯示
<?php
op_include_form('vote_question_from',$form,array('title'=>'編輯問題','url'=>url_for('@vote_update?id='.$form->getObject()->getId()),));
?>包含一個表單對象,'vote_question_from'為標識名,$form為對應(yīng)動作傳來的表單對象,第三個數(shù)組參數(shù)title鍵值也url鍵值是固定的,分別對應(yīng)顯示的標題名和表單提交路徑
對應(yīng)動作內(nèi)容為
<?php
public function executeEdit(sfWebRequest $request){
$object = $this->getRoute()->getObject();
//如果不是作者屏幕上顯示404
$this->forward404Unless($this->getUser()->getMemberId() == $object->getMemberId());//$object->getMemberId()為傳遞過來的id值對應(yīng)的那條記錄的member_id字段值
$this->form = new VoteQuestionForm($object);
//訪問此動作路徑http://localhost/openpne/web/vote/edit/1
}
?>
<?php op_include_pager_navigation($pager, '@tasks_list?page=%d'); ?>用于分頁時前后翻頁的超鏈接
$pager來自動作里的 $this->pager = Doctrine::getTable('VoteQuestion')->getListPager($request->getParameter('page'));
PluginVoteQuestionTable類getListPager()方法里的內(nèi)容↓
<?php
class PluginVoteQuestionTable extends Doctrine_Table
{
public function getListPager($page = 1,$size = 20)
{
$query = $this->createQuery()->orderBy('updated_at DESC');
$pager = new sfDoctrinePager('VoteQuestion',$size);//創(chuàng)建一個某表的分頁對象,并傳一個每頁顯示多少記錄值
$pager->setQuery($query);//傳一個查詢語句對象
$pager->setPage($page);//設(shè)返回顯示的頁數(shù)
$pager->init();
return $pager;
}
}
?>
對應(yīng)前臺頁面對分頁結(jié)果集的瀝遍
<?php foreach($pager->getResults() as $item): //利用openPNE分頁機制獲取指定分頁結(jié)果集并瀝遍每一條記錄?>
<dl>
<dt><?php echo op_format_date($item->getUpdatedAt(),'f') //'f'代表一種顯示格式?></dt><!--op_format_date()方法只是把2011-11-10各種中的‘-'換成漢字年月日-->
<dd><?php echo link_to(sprintf("%s(%d)",$item->getTitle(),count($item->getVoteAnswers())),'@vote_show?id='.$item->getId()) ?></dd><!--$item->getTitle()獲取該條記錄指定字段title值-->
</dl>
<?php endforeach; ?>
<?php echo link_to('sdsfg','@vote_show?id='.$item->getId()) ?>相當于<a href='vote/show?id=...'>sdsfg</a>
表名是駝峰模式在數(shù)據(jù)庫里以下劃線表示,字段名也是如此
鏈接的
就算不用方法也可以直接在action="此可直接寫web/后的====模塊名/動作名====或路由中設(shè)定好的web后的路徑"
動作里的
$this->tasksObject = $this->getRoute()->getObject();
$this->getRoute()->getObject();//獲取傳過來的id參數(shù)值對應(yīng)的表中的那條信息對象可通過get+字段名()獲取字段值,如在頁面中$tasksObject-getId();
至于如何確定獲取的是哪個表則是通過路由類設(shè)置該動作路由時確定的,如下例確定的是vote_question表
例
<?php
class opVotePluginFrontendRouteCollection extends sfRouteCollection
{
public function __construct(array $options)
{
parent::__construct($options);
$this->routes = array(
'vote_edit' => new sfDoctrineRoute(
'/vote/edit/:id',
array('module' => 'vote', 'action' => 'edit'),
array('id' => '\d+', 'sf_method' => array('get')),
array('model' => 'VoteQuestion', 'type' => 'object')
),
);
}
}
?>
相關(guān)文章
PHP中include()與require()的區(qū)別說明
引用文件的方法有兩種:require 及 include。兩種方式提供不同的使用彈性。2010-03-03適用于php-5.2?的?php.ini?中文版[金步國翻譯]
最近服務(wù)器基本上都是使用了php?5.2.*,以前發(fā)布的版本的php.ini已經(jīng)不適用于現(xiàn)在的版本了,特發(fā)現(xiàn)了這篇文章特分享給大家。2011-04-04