【问题标题】:Doctrine substring field with alias in where clause在 where 子句中具有别名的 Doctrine 子字符串字段
【发布时间】:2017-07-12 14:31:50
【问题描述】:

我有以下疑问:

$query = $qb->select($qb->expr()->substring("p.website",1,5).'AS country)
        ->from("AppBundle\Entity\Image" ,"p")
        ->where("p.aktuellste = 1")
        ->andWhere($qb->expr()->in('country',':country'))
        ->setParameter(':country','de')
        ->orderBy('country','DESC')
        ->getQuery();

例如我想选择网站的substring(1,2)de 的所有行。但是countryWHERE 子句中 Doctrine 的未知列。抛出以下异常:

SQLSTATE[42S22]:未找到列:1054 中的未知列 'sclr_0' 'where子句

有趣的是,Doctrine 知道 ORDER BY 子句中的 country 列。

有人知道如何解决这个问题吗?

【问题讨论】:

  • 不确定这是否有帮助,但在所有 where 表达式中使用前缀 p。并且 MySQL 知道 order 子句中的列“country”,因为“临时”表(在 select 执行之后)具有字段 country。 (如果理解有误,请指正!!)
  • 嘿,谢谢,但我已经试过这个了。问题是,学说在我的“图像”类中需要“国家”字段。

标签: php symfony doctrine-orm doctrine


【解决方案1】:

如果要搜索别名列,则需要使用HAVING,因为这是在计算字段上。将它放在您的 WHERE 子句中是假设您的 Image 实体中有一个名为 country 的成员,但它没有。

$query = $qb
    ->select($qb->expr()->substring('p.website', 1, 5).'AS country')
    ->from('AppBundle\Entity\Image', 'p')
    ->andWhere('p.aktuellste = 1')
    ->andHaving($qb->expr()->in('country', ':country'))
    ->setParameter(':country', 'de')
    ->orderBy('country', 'DESC')
    ->getQuery()
;

更多信息请见MySQL Handling of Group By

MySQL 扩展允许在 HAVING 子句中使用别名 对于聚合列

【讨论】:

    【解决方案2】:

    您的查询中有一些语法错误,但不确定这是否能解决您的问题。 请试试这个:

    $query = $qb->select($qb->expr()->substring("p.website",1,5).'AS country')
        ->from("AppBundle\Entity\Image" ,"p")
        ->where("p.aktuellste = 1")
        ->andWhere($qb->expr()->in('country=:country'))
        ->setParameter('country','de')
        ->orderBy('country','DESC')
        ->getQuery();
    

    【讨论】:

    • 谢谢,但我不认为我有语法错误,因为您的查询因“IN 表达式”中缺少参数而失败 ->andWhere($qb->expr()->in('country=:国家'))
    猜你喜欢
    • 2011-05-22
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    相关资源
    最近更新 更多