【问题标题】:Connect With PostgreSQL in Phalcon Framework在 Phalcon 框架中连接 PostgreSQL
【发布时间】:2015-04-16 12:02:12
【问题描述】:

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

【问题讨论】:

  • 您是否收到任何错误消息?
  • 好的,但配置还不够。给我们您的$di->db 服务实施和使用案例。至少也应该显示一些错误。
  • 在 index.php 中添加 ini_set('display_errors', 1);。您还需要将 DbAdapter 更改为 Phalcon\Db\Adapter\Pdo\Postgresql。我们还需要了解您的服务是如何设置的。
  • 使用 phalcon 调试器(在 public/index.php 中没有 try-catch)并向我们显示错误。

标签: postgresql phalcon


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我认为该代码很好。 我正在做一个项目,我必须使用 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;
    
    }
    

    为了更好地理解,我建议您访问url

    需要澄清的一件事-我未能通过 Model for PostGreSQL 进行数据库操作。这就是我使用这种方式的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      相关资源
      最近更新 更多