我试图将我的Behat测试从Jenkins(每次运行构建服务器)移动到Docker,以便我可以将它添加到Travis。
我遇到了一个小问题,其中查询的WHERE
部分未被Doctrine插入。 DQL输出是这样的:
SELECT v FROM \Database\Entity\SoftwareVersion v WHERE ORDER BY v.versionMajor DESC, v.versionMinor DESC, v.versionPatch DESC, v.createdAt DESC
正如您所见,WHERE
和ORDER
之间没有任何内容。
查询构建器代码如下所示:
/**
* 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();
}
我知道,它看起来很复杂,但即使我们采取前几行:
$builder
->select('v')
->from('\Mischief\Database\Entity\SoftwareVersion', 'v')
->andWhere('v.deletedAt IS NULL')
它仍然无法运作。
更有趣的是:此代码是实时的,并且按预期运行。我检查过项目的依赖关系是一样的。
我的PHP容器的docker文件是:
FROM php:7-fpm
RUN docker-php-ext-install pdo pdo_mysql mbstring
我无法看到现场的任何模块。
那么WHERE
在哪里? :)
请询问我是否可以提供更多信息。
答案 0 :(得分:0)
$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())
;