【问题标题】:Doctrine Querybuilder, binding ParametersDoctrine Querybuilder,绑定参数
【发布时间】:2014-08-22 16:26:13
【问题描述】:

我的 QueryManager 的选择功能:

/**
 * Führt eine SELECT - Query durch
 *
 * @param   $select = array( array(column, [...]), table, shortcut )
 *          $orderby = array(column, sorting-type)
 *          $where = array( array( column, value, type[or, and] ), [...] )
 *          $innerjoin = array( table, shortcut, condition )
 *          $pagination = array( page, limit )
 * 
 * @return array $data
 */
public function select($select,$orderby, $where, $innerjoin, $pagination)
{
    $qb = $this->conn->createQueryBuilder()
        ->select($select[0])
        ->from($select[1], $select[2])
    ;

    if ($orderby) {
        $qb->orderBy($orderby);
    }

    if ($where) {
        foreach($where as $cond) {
            $x = 0;
            if ( key($cond) == 0 ) {
                $qb
                    ->where($cond[0] . ' = ?')
                    ->setParameter($x,$cond[1]);
            }
            elseif ( $cond[2] == 'and' ) {
                $qb
                    ->andWhere($cond[0] . ' = ?')
                    ->setParameter($x,$cond[1]);
            }
            elseif ( $cond[2] == 'and' ) {
                $qb
                    ->orWhere($cond[0] . ' = :' . $x)
                    ->setParameter($x,$cond[1]);
            }
            $x++;
        }
    }

    if ($innerjoin) {
        $qb->join($select[2],$innerjoin);
    }

    $this->sql = $qb->getSQL();

    $this->totalRowCount = count( $qb->execute() ) ;

    if ($pagination) {
        $max = $pagination[0] * $pagination[1];
        $first =  $max - $limit;

        $qb
            ->setFirstResult($first)
            ->setMaxResults($max)
        ;
    }

    $stmt = $qb->execute();
    return $stmt->fetchAll();
}

我不知道为什么,但实际上,这个函数会生成一个选择查询,但没有为参数插入值:

/**
 * Lädt einen User nach dessen Username
 *
 * @param $username
 * @return User $user | null
 */
public function getUser($username)
{
    if($data = $this->select(array('*','users','u'), null, array( array('username',$username) ), null,null)) {
        return $user = $this->hydrate($data);
    }
    return null;
}

我没有得到结果,并且查询设置不正确:

array(0) { }
SELECT * FROM users u WHERE username = ?

在我看来,Builder 不会用提供的值代替我的参数...

我得到了最新版本的 Doctrine DBAL (2.4),这个版本应该支持这个功能!

感谢您的帮助和建议:)

【问题讨论】:

    标签: doctrine-orm


    【解决方案1】:

    我也有这个问题。我在这里读到了doctrine 2 querybuilder with set parameters not working

    您不能将参数绑定到 QueryBuilder,只能绑定到 Query

    但是我在深层嵌套对象中创建 SQL 条件作为收集的 AND & OR experssions,并且最顶层的对象创建查询对象。所以我之前不能创建查询对象,我总是返回表达式对象。

    所以我解决了直接将变量包含到准备变量的位置的问题。

    $qb->where($cond[0] . '=' . $cond[1]);
    

    因为我期望字符串在那里,所以我添加了硬编码引号。这不是理想的方式,但目前我不知道如何通过将参数绑定到 QueryBuilder 对象以其他方式解决这个问题。

    $expr = $d_qb->expr()->between($t_c, "'" . $date_from . "'", "'" . $date_from . "'");
    

    其他建议?

    以下代码结果:

    $expr = $d_qb->expr()->between($t_c, ':from', ':to');
    $d_qb->setParameter('from', 1);
    $d_qb->setParameter('to', 1);
    

    $expr = $d_qb->expr()->between($t_c, ':from', ':to');
    $d_qb->setParameter(':from', 1);
    $d_qb->setParameter(':to', 1);
    

    结果:

    e0_.created BETWEEN ? AND ?
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 2018-09-21
      • 2012-11-18
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多