[英]Doctrine missing “WHERE” clause
I'm attempting to move my Behat tests from Jenkins (which builds a server each run), to Docker, so that I can add it to Travis. 我试图将Behat测试从Jenkins(每次运行都会构建一个服务器)移至Docker,以便可以将其添加到Travis。
I've run in to a little problem where the WHERE
part of a query isn't being inserted by Doctrine. 我遇到了一个小问题,即Doctrine没有插入查询的WHERE
部分。 The DQL output is this: DQL输出是这样的:
SELECT v FROM \Database\Entity\SoftwareVersion v WHERE ORDER BY v.versionMajor DESC, v.versionMinor DESC, v.versionPatch DESC, v.createdAt DESC
As you can see, nothing between WHERE
and ORDER
. 如您所见, WHERE
和ORDER
之间没有任何关系。
The query builder code looks like this: 查询构建器代码如下所示:
/**
* Get the criteria array for versions
* @param SoftwareProduct $product Required. The product to check versions for
* @param SoftwareOperatingSystem $operatingSystem Required. The operating system you are using.
* @param string $version Optional. Approximate or exact version to check for. Eg 2.0 Might return 2.0.2
* @param string $order Should be ASC or DESC. Default: DESC
* @param int|null $limit How many results you want. Leave blank for all
* @return SoftwareVersion[]
* @throws Exception
*/
public function getVersions(
SoftwareProduct $product,
SoftwareOperatingSystem $operatingSystem = null,
$version = null,
$order = 'DESC',
$limit = null
) {
if ($order != 'DESC' && $order != 'ASC') {
throw new \DomainException("Invalid order '$order'");
}
$builder = $this->getEntityManager()
->createQueryBuilder();
$builder
->select('v')
->from('\Database\Entity\SoftwareVersion', 'v')
->andWhere('v.deletedAt IS NULL')
->andWhere($builder->expr()->lt('v.releaseDate', ':releaseDate'))
->setParameter('releaseDate', date('Y-m-d H:i:s'))
->andWhere($builder->expr()->eq('v.product', ':product'))
->setParameter('product', $product->getId())
;
// Anonymous function for simplified code
$equalIfExists = function ($key, $value) use ($builder) {
if ($value !== null) {
$builder
->andWhere($builder->expr()->eq("v.$key", ":$key"))
->setParameter($key, $value);
}
};
// OS
$equalIfExists('operatingSystem', $operatingSystem);
// Version Number
$versionObject = $this->stringToVersion($version);
$equalIfExists('versionMajor', $versionObject->getVersionMajor());
$equalIfExists('versionMinor', $versionObject->getVersionMinor());
$equalIfExists('versionPatch', $versionObject->getVersionPatch());
// Version Meta
$builder
->andWhere($builder->expr()->like("v.meta", ":meta"))
->setParameter('meta', $versionObject->getMeta() ? "{$versionObject->getMeta()}%" : '');
$builder
->addOrderBy('v.versionMajor', $order)
->addOrderBy('v.versionMinor', $order)
->addOrderBy('v.versionPatch', $order)
->addOrderBy('v.createdAt', $order);
$query = $builder->getQuery();
if ($limit) {
$query->setMaxResults($limit);
}
return $query->execute();
}
I know, it looks pretty complicated, but even if we take the first few lines: 我知道,这看起来很复杂,但是即使我们采用前几行:
$builder
->select('v')
->from('\Mischief\Database\Entity\SoftwareVersion', 'v')
->andWhere('v.deletedAt IS NULL')
It still doesn't work. 它仍然不起作用。
What's more interesting: this code is live and functions as expected. 更有趣的是:该代码是实时的,可以按预期运行。 I've checked the project dependencies are the same. 我检查了项目依赖项是否相同。
The docker file for my PHP container is this: 我的PHP容器的docker文件是这样的:
FROM php:7-fpm
RUN docker-php-ext-install pdo pdo_mysql mbstring
I can't see any modules on live that aren't in here. 我看不到现场没有的任何模块。
So where is my WHERE
? 那么我的位置在WHERE
? :) :)
Please ask if I can offer any more information. 请询问我是否可以提供更多信息。
$builder
->select('v')
->from('\Database\Entity\SoftwareVersion', 'v')
->where('v.deletedAt IS NULL')
->andwhere($builder->expr()->lt('v.releaseDate', ':releaseDate'))
->andwhere($builder->expr()->eq('v.product', ':product'))
->setParameter('releaseDate', date('Y-m-d H:i:s'))
->setParameter('product', $product->getId())
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.