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 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM