欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

openPNE常用方法分享

程序员文章站 2022-07-07 08:25:27
复制代码 代码如下: 'asdfgasgsad'));?>这...
复制代码 代码如下:

<?php include_partial('sidemenu',array('form'=>'asdfgasgsad'));?>这句话意思是包含'_sidemenu.php'并往其页面传一系列参数,'_sidemenu.php'页即可直接使用$form变量中的值
<?php
op_include_box('vote_question_create_box','<strong>asdfasdf</strong>',array('title'=>'创建问题','moreinfo'=>array('创建问题',link_to('创建问题2','@my_index'))));
?>
<?php
op_include_box('vote_question_create_box',get_slot('pager'),array('title'=>'创建问题','moreinfo'=>array('创建问题',link_to('创建问题2','@my_index'))));
?>
'vote_question_create_box'只是一个标记,'<strong>asdfasdf</strong>'或 get_slot('pager')则是要输出到页面上标题下的信息(这个方法里要包含slot只能用get_slot()不能用include_slot(),
而在页面中要包含slot则必须使用include_slot())
第三个数组参数中的键值名称title是固定的,是该段'vote_question_create_box'显示的标题,后面的'moreinfo'键名也是固定键值对应的数组则是罗列显示的内容列表

<?php slot('pager'); ?>设定一个slot段落
<?php echo 'asdfasgsadfasdfaaaaaaaaaaaaaaaaaaaaaa' ?>
<?php end_slot() ?>
<?php include_slot('pager'); ?>包含指定的slot段落,设定的slot段落必须通过包含才能在页面上显示

<?php
op_include_form('vote_question_from',$form,array('title'=>'编辑问题','url'=>url_for('@vote_update?id='.$form->getobject()->getid()),));
?>包含一个表单对象,'vote_question_from'为标识名,$form为对应动作传来的表单对象,第三个数组参数title键值也url键值是固定的,分别对应显示的标题名和表单提交路径
对应动作内容为
<?php
public function executeedit(sfwebrequest $request){
$object = $this->getroute()->getobject();
//如果不是作者屏幕上显示404
$this->forward404unless($this->getuser()->getmemberid() == $object->getmemberid());//$object->getmemberid()为传递过来的id值对应的那条记录的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()方法里的内容↓
<?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);//创建一个某表的分页对象,并传一个每页显示多少记录值
$pager->setquery($query);//传一个查询语句对象
$pager->setpage($page);//设返回显示的页数
$pager->init();
return $pager;
}
}
?>
对应前台页面对分页结果集的沥遍
<?php foreach($pager->getresults() as $item): //利用openpne分页机制获取指定分页结果集并沥遍每一条记录?>
<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>
表名是驼峰模式在数据库里以下划线表示,字段名也是如此

链接的
就算不用方法也可以直接在action="此可直接写web/后的====模块名/动作名====或路由中设定好的web后的路径"

动作里的
$this->tasksobject = $this->getroute()->getobject();
$this->getroute()->getobject();//获取传过来的id参数值对应的表中的那条信息对象可通过get+字段名()获取字段值,如在页面中$tasksobject-getid();
至于如何确定获取的是哪个表则是通过路由类设置该动作路由时确定的,如下例确定的是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')
),
);
}
}
?>