1

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.
0

1 Answer 1

2

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

->where('u.priceRanges LIKE :parameterName')
->setParameter('parameterName', 100);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.