NGrdanjski
NGrdanjski

Reputation: 151

Symfony - Doctrine createQueryBuilder from json_array

Crete custom query in Symfony 4 and I use createQueryBuilder(). My entity have column price_ranges type type="json_array"

This data I store in this column

{
    "from": "10.10.2010", 
    "to": "10.20.2010", 
    "pricePerNight": 100,
    "minStay": 7
}

Want to query data by pricePerNight key in price_ranges column.

I create function but I have the following error:

[Semantical Error] line 0, col 41 near 'price_ranges': Error: Class App\Entity\House has no field or association named price_ranges

Here is my function. Where am I wrong?

public function findDataByPriceRange()
    {
        $qb = $this->createQueryBuilder('u');
        $qb->select('u')
            ->where('u.price_ranges LIKE :price_ranges')
            ->setParameter('pricePerNight', 100);
        return $qb->getQuery()->getResult();
    }

After I edit my function:

public function findVillasByPriceRange()
{
    $qb = $this->createQueryBuilder('u');
    $qb->select('u')
        ->where('u.priceRanges LIKE :priceRanges')
        ->setParameter('pricePerNight', 100);
    return $qb->getQuery()->getResult();
}

I get this error:

Invalid parameter: token pricePerNight is not defined in the query.

Upvotes: 1

Views: 766

Answers (1)

Yoann Kergall
Yoann Kergall

Reputation: 3302

You need to use the same parameter name in where() and setParameter() like this

->where('u.priceRanges LIKE :parameterName')
->setParameter('parameterName', 100);

Upvotes: 2

Related Questions