简体   繁体   中英

Symfony Doctrine: Search in simple_array

I got values stored in my database column field as value1,value2,value3,value4 , so a simple_array column.

So i'm using Doctrine to make a search using this:

$searchQuery = $this->getDoctrine()
        ->getRepository('AppBundle:Ads')
        ->createQueryBuilder('p')
        ->andWhere("p.vals <= :value2")
        ->setParameter('value2',  $request->query->get('value2'));
        ->orderBy("p.creationtime", 'DESC');

So expecting value2 is in the 2nd position of a simple array like value1,value2,value3 , how can i ask QueryBuilder to select the second value in the string?

I think this query try to get all the values in p.vals , results are not right, shound select just one.

How can I select eg. the 2nd value in p.vals ?

I believe you cannot access nth item of an array column using pure Mysql since the data is serialized, in order to do it I'd create a simple function

public function getItemFromArray(array $array, $index)
{
    return isset($array[$index]) ? $array[$index] : null;
}

And if you want to find item with condition use

array_filter()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM