简体   繁体   中英

How to search a word in doctrine ORM?

I would like to know if this is possible in doctrine ORM symfony. I have a list of Products. Each product has a Title. let say their titles are:

1. Great Mango
2. Show Mango
3. King Mango

I use this doctrine to find the title

$repository->findByTitle("Mango");

but I display nothing. If I complete the title to search it like "King Mango". It display but not the list with the MANGO word.

I used this link for reference. but if you have more documentation about this. I'm happy to learn from it.

The magic methods only work with exact search. To use a LIKE statement, you'll have to create your own repository, as described here: http://symfony.com/doc/current/doctrine/repository.html

Once you have your own repository class, create a method that uses LIKE in the query:

public function findByTitlePart($title)
{
    return $this->getEntityManager()
        ->createQuery(
            'SELECT p FROM AppBundle:Product p WHERE p.title LIKE :title'
        )
        ->setParameter('title', '%' . $title . '%')
        ->getResult();
}

To use a like in the where clause, you have to use where condition with setParameter . query as below:

$title = 'Mango';
$query = $repository->createQueryBuilder('a')
           ->where('a.title LIKE :title')
           ->setParameter('title', '%'.$title.'%')
           ->getQuery();

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