目前我决定将Phalcon php作为Codeigniter的替代php框架.我按照网站上的教程进行操作,它的工作方式非常好.我仍然试图绕过一些事情.
根据我的理解,模型与数据库绑定并映射到数据库中的表.我有一个项目,我需要使用2个或更多数据库.该项目有一个后端(一个数据库)和多个前端(另一个数据库).最简单的方法是运行自定义MySQL查询以从多个数据库中获取数据.我不知道如何从Phalcon的模型中做到这一点.我查看了stackoverflow,尝试了一些建议,但仍然没有运气.
我猜应该有一些简单的方法可以从模型中进行,例如$result = $this-> query(“SELECT * FROM backend.users”) – > fetch();但它不起作用.
在这里我有:
控制器:
class SignupController extends \Phalcon\Mvc\Controller{
function indexAction()
{
}
function registerAction()
{
$user = new Users();
$result=$user->saveNewUser();
print_r($result); // Nothing
//$result=$this->db->query("SELECT * FROM phalcon.system_users")->fetchAll();
//print_r($result); // Works
$this->view->disable();
}
}
模型:
class Users extends Phalcon\Mvc\Model
{
public function saveNewUser()
{
return $this->db; // how to run the query???
}
}
引导:
try {
//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
'../app/controllers/',
'../app/models/'
))->register();
//Create a DI
$di = new Phalcon\DI\FactoryDefault();
//Setup the database service
$di->set('db', function(){
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "localhost",
"username" => "root",
"password" => "123456",
"dbname" => ""
));
});
//Setup the view component
$di->set('view', function(){
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views/');
return $view;
});
//Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set('url', function(){
$url = new \Phalcon\Mvc\Url();
$url->setBaseUri('/phalcon/');
return $url;
});
//Handle the request
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
} catch(\Phalcon\Exception $e) {
echo "PhalconException: ", $e->getMessage();
}
我喜欢Codeigniter拥有它的方式,不确定Phalcon是否有一种简单的方法.可能是我需要加载扩展或库来在模型中执行此操作.
提前致谢!