phalcon:官方多模块支models层,mode数据库配置(二)
利用:pahlconmvcmodelManager::registerNamespaceAlias()方法获取多模块下的model层
public/index.php
use PhalconMvcModelManager as ModelsManager; //对模型进行别名处理 $di->set('modelsManager', function() { return new ModelsManager(); });
model层,我的命名空间是:
namespace AppModulesBackendModels;
namespace AppModulesBackendModels; use PhalconMvcModel; class Album extends Model { //id public $aid; //模板id public $atid; public $name; //影片id public $mid; //文章id public $nid; public $enable; public $create_time; public function beforeCreate() { if( is_null($this->create_time) ) $this->create_time = time(); if( is_null($this->enable) ) $this->enable = 0; } }
那么controller层怎么调用model数据呢。
方法有一:
$cate = AppModulesBackendModelsAlbum::findFirst(array( 'conditions'=>"aid = :aid:", 'bind'=>array('aid'=>$aid) ));
方法二(多表查询):
注册别名
$this->modelsManager->registerNamespaceAlias('m' , 'AppModulesBackendModels'); $currentPage = $this->getParam('page'); $builder = $this->modelsManager->createBuilder() ->columns("aid,atid,name,mid,nid,create_time") ->from("m:Album") ->where("enable = 0") ->orderBy("aid ASC");
将model下的文件,都赋给别名:m,那么调用具体的数据表就是: m:Album, m:User。。。。。。