繁体   English   中英

在Phalcon Framework中与PostgreSQL连接

[英]Connect With PostgreSQL in Phalcon Framework

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
    ));
});

您使用的数据库连接阵列中没有端口 默认的postgresql端口是5432 所以在你的数据库连接中使用它,并尝试将数组直接传递给postgresql适配器构造函数。 或再次安装postgres pdo / php pdo。

我认为该代码很好。 我当时正在一个必须使用MySQL和PostGres的项目。我通过以下方式连接了Postgresdb:

在默认的“ 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;

}

为了更好的理解,我建议您访问此网址

需要澄清的一件事-我无法通过Model for PostGreSQL进行数据库操作。 这就是为什么我用这种方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM