Symfony:主义-检查是否存在连接 [英] Symfony: Doctrine - checking there's a connection

查看:39
本文介绍了Symfony:主义-检查是否存在连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以调试为什么Doctrine没有连接到MySQL?

Is there a simple way to debug why Doctrine is not connecting to MySQL?

config.yml 具有:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"

parameters.yml 似乎具有正确的连接信息.例如

and parameters.yml seems to have the correct connection information in it. E.g.

parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: <my database name>
    database_user: <my database user>
    database_password: <my database password>

但是,这段代码仍然回显未连接".

However this piece of code still echoes out "Not connected".

    $cnx = $this->getDoctrine()->getConnection();
    if ($cnx->isConnected()){
        echo "Connected";
    }
    else {
        echo "Not connected";
    }

,我没有得到任何错误返回.

and I'm not getting any errors returned.

有什么建议吗?

推荐答案

这有效:

$em = $this->getDoctrine()->getManager();
$em->getConnection()->connect();
$connected = $em->getConnection()->isConnected();

$ connected将为 true 表示已连接.

$connected will be true to indicate it is connected.

connect()建立连接,然后 isConnected()返回一个布尔值以告诉它是否已连接.

The connect() establishes the connection, and then isConnected() returns a boolean to tell if it is connected.

这篇关于Symfony:主义-检查是否存在连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆