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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站