php phacon 关联模型吗,php – Phalcon在模型中执行sql查询

目前我决定将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是否有一种简单的方法.可能是我需要加载扩展或库来在模型中执行此操作.

提前致谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值