在Phalcon框架中连接PostgreSQL

在Phalcon框架中连接PostgreSQL,postgresql,phalcon,Postgresql,Phalcon,Phalcon无法连接到postgrsql。以下是我在config.php中的设置 return new \Phalcon\Config(array( 'database' => array( 'adapter' => 'Postgresql', 'host' => 'localhost', 'username' => 'postgres', 'password' =

Phalcon无法连接到postgrsql。以下是我在config.php中的设置

return new \Phalcon\Config(array(
    'database' => array(
        'adapter'     => 'Postgresql',
        'host'        => 'localhost',
        'username'    => 'postgres',
        'password'    => 'root',
        'dbname'      => 'mydb',
        'charset'     => 'utf8',
    ),
    'application' => array(
        'controllersDir' => __DIR__ . '/../../app/controllers/',
        'modelsDir'      => __DIR__ . '/../../app/models/',
        'viewsDir'       => __DIR__ . '/../../app/views/',
        'pluginsDir'     => __DIR__ . '/../../app/plugins/',
        'libraryDir'     => __DIR__ . '/../../app/library/',
        'cacheDir'       => __DIR__ . '/../../app/cache/',
        'baseUri'        => '/test/',
    )
));
页面为空白,未显示任何错误

DI服务实现

use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter;

$di->set('db', function () use ($config) {
    return new DbAdapter(array(
        'host' => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->dbname,
        "charset" => $config->database->charset
    ));
});
您使用的db连接阵列中没有端口。默认的postgresql端口是5432。所以,在db连接中使用它,并尝试将数组直接传递给postgresql适配器构造函数。 或 再次安装postgres pdo/php pdo。

我认为代码很好。 我在做一个项目,我必须使用MySQL和PostGres-

public function yourcontrollernameAction()
{

   $con = $this->di->get('pghalcon');
   $post = $con->fetchOne("SELECT * FROM blog ORDER BY id DESC LIMIT 1", Phalcon\Db::FETCH_ASSOC);
   $this->view->data= $post;

}
在默认的“db”连接字符串下面,我在services.php中编写了以下代码-

$di->setShared('pgphalcon', function () {
$config = $this->getConfig();
$adaptar = "PostgreSQL";
$class = 'Phalcon\Db\Adapter\Pdo\\' . $adaptar;
$params = [
    'host'     => "localhost",
    'port'     => "5432", // for localhost no need to put this line unless u change the port
    'username' => "postgres",
    'password' => "12345",
    'dbname'   => "phalcon",

];
$connection = new $class($params);
return $connection;
})

然后你的控制器开始工作

执行以下操作-

public function yourcontrollernameAction()
{

   $con = $this->di->get('pghalcon');
   $post = $con->fetchOne("SELECT * FROM blog ORDER BY id DESC LIMIT 1", Phalcon\Db::FETCH_ASSOC);
   $this->view->data= $post;

}
为了更好地理解,我建议您访问本网站


有一件事需要澄清——我没有按照PostGreSQL的模型执行db操作。这就是我使用这种方式的原因。

您是否收到任何错误消息?好的,但配置是不够的。请提供您的$di->db服务实现和使用案例。此外,至少还应显示一些错误。添加ini_集合“显示错误”,1;在index.php中。您还需要将DbAdapter更改为Phalcon\Db\Adapter\Pdo\Postgresql。我们还需要了解您的服务是如何设置的。在public/index.php中使用phalcon调试器而不使用try catch,并向我们显示错误。