phalcon:整合官方多模块功能,方便多表查询
Posted 穆晟铭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了phalcon:整合官方多模块功能,方便多表查询相关的知识,希望对你有一定的参考价值。
phalcon:整合官方多模块功能,方便多表查询
项目分为:
namespace Multiple\\Backend;
namespace Multiple\\Frontend;
目录结构如下:
public/index.php的大致写法:
多模块功能:
// Handle the request $application = new Application($di); //加入模块分组配置 $application->registerModules( array( \'frontend\' => array( \'className\' => \'Multiple\\Frontend\\Module\', \'path\' => \'../app/frontend/Module.php\', ), \'backend\' => array( \'className\' => \'Multiple\\Backend\\Module\', \'path\' => \'../app/backend/Module.php\', ) ) );
来看下多模块下Module.php的写法,
backend/Module.php
namespace Multiple\\Backend; use Phalcon\\Loader, Phalcon\\Mvc\\Dispatcher, Phalcon\\DiInterface, Phalcon\\Mvc\\View, Phalcon\\Mvc\\ModuleDefinitionInterface; class Module implements ModuleDefinitionInterface { public function registerAutoloaders( DiInterface $di = NULL) { $loader = new Loader(); $loader->registerNamespaces(array( \'Multiple\\Backend\\Controllers\' => __DIR__ .\'/controllers/\' ))->register(); $loader->registerDirs( array( \'modelsDir\' => \'../app/models/\', #注意这里,必须填写,否则models/下的文件不能共用。 ) )->register(); } public function registerServices( \\Phalcon\\DiInterface $di) { $di->set("dispatcher", function(){ $dispatcher = new Dispatcher(); $dispatcher->setDefaultController("Multiple\\Backend\\Controllers"); return $dispatcher; }); $di->set("view", function(){ $view = new View(); $view->setViewsDir("../app/backend/views/"); $view->registerEngines(array( \'.phtml\' => \'Phalcon\\Mvc\\View\\Engine\\Php\' )); return $view; }); } }
models/下的model文件,不需要命名空间,直接写:
use \\Phalcon\\Mvc\\Model; class Album extends Model { ...... }
controllers/下面的model调用:
namespace Multiple\\Backend\\Controllers; use Phalcon\\Paginator\\Adapter\\QueryBuilder as PaginatorQueryBuilder; class AlbumController extends ControllerBase { public function initialize(){ parent::initialize(); } public function indexAction() { $currentPage = $this->getParam(\'page\'); $builder = $this->modelsManager->createBuilder() ->columns("aid,atid,name,mid,nid,create_time") ->from("Album") ->where("enable = 0") ->orderBy("aid ASC"); $paginator = new PaginatorQueryBuilder(array( \'builder\' => $builder, \'limit\' => 10, \'page\' => $currentPage )); $category = \'\'; if( $this->getAlbumCategory() ) { foreach($this->getAlbumCategory() as $k=>$v) { $category[$v[\'atid\']] = $v; } } }
以上是关于phalcon:整合官方多模块功能,方便多表查询的主要内容,如果未能解决你的问题,请参考以下文章