dqyuipw44576
2018-04-11 09:05
浏览 29
已采纳

Doctrine QueryBuilder未定义方法getQuery()

When I execute the method getMachineSettings I get an error:

Fatal error: Uncaught Error: Call to undefined method Doctrine\DBAL\Query\QueryBuilder::getQuery()

$data is an associative array:

$data['param'] = 'ip';
$data['value'] = '192.168.240.10';

If I replace getQuery()->getResult() with execute(), $result contains the query:

SELECT * FROM machine WHERE ip = ?

public function __construct()
{

    try
    {

        $dbconf = parse_ini_file('.htLogin.ini');
        $config = new \Doctrine\DBAL\Configuration();

        $connectionParams = array
            (
                'dbname'    => $dbconf['infoDb'],
                'user'      => $dbconf['infoLogin'],
                'password'  => $dbconf['infoPw'],
                'host'      => $dbconf['infoHost'],
                'driver'    => 'pdo_mysql',
                'charset'   => 'utf8',
                'driverOptions' => array
                    (
                        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
                    )

            );
        $this->mysql = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);

    }
    catch(PDOException $e)
    {

        echo $e -> getMessage();

    }


public function getMachineSettings($data)
{
    $qb = $this->mysql->createQueryBuilder();
    $qb->SELECT('*')
       ->FROM('`machine`')
       ->WHERE($data['param'] . ' = :value');
    $qb->setParameters(
            array
            (
                ':value' => $data['value']
            )
        );

    $results = $qb->getQuery()->getResult();


    var_dump($result);

    return $result;

 }

Do you have any idea why the method getQuery() is not recognised?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanlan2003 2018-04-11 09:31
    已采纳

    Just do

    $results = $qb->execute->fetchAll();
    

    Ignore the below - it assumes that your using the Doctrine ORM, which your not

    The issue your having is that the QueryBuilder object your working with isn't the Doctrine ORM QueryBuilder - its the DBAL QueryBuilder.

    You need to use the createQueryBuilder function from the EntityManager.

    /** @var Doctrine\ORM\QueryBuilder $qb */
    $qb = $this->entityManager->createQueryBuilder();
    

    Then you can use the select / from etc methods and to get the result of the query you can run

    $qb->getQuery()->getResult()
    

    If I was to rewrite your function I would write it like this

    public function getMachineSettings(string $field, string $value)
    {
        $qb = $this->entityManager->createQueryBuilder();
        $qb->select('m')
           ->from('machine')
           ->where($field.' = :value');
        $qb->setParameter('value', $value);
        $results = $qb->getQuery()->getResult();
        var_dump($result);
        return $result;
    }
    

    Then you know that the function requires 2 parameters to function, passing an array doesn't let you immediately see what the function requires

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题