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.

I’ve run in to a little problem where the WHERE part of a query isn’t being inserted by Doctrine. The DQL output is this:

  • Testing a Docker Image
  • How to clone and customise laravel homestead and install more things in it?
  • Vagrant - Docker provider vs. docker provisioner
  • Scripts in shell script not being executed in docker
  • How to make request to server running on docker
  • XDummy in Docker container
  • 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.

    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:

    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? 🙂

    Please ask if I can offer any more information.

  • Error when run clickhouse docker image on mac os
  • Spotify docker maven build multiple images
  • Docker network (namespaces): how to tag network packages?
  • Addressing issues with Apache Spark application run in Client mode from Docker container
  • What is the `<<-EOSQL` code block in Bash when running SQL?
  • I got an error when I try to delete an image in docker
  • One Solution collect form web for “Doctrine missing “WHERE” clause”

    $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())
        ;
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.