如何在学说ORM中搜索一个单词?

如何在学说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.

我想知道这是否可以在学说ORM symfony中使用。 我有一个产品列表。 每个产品都有一个标题。 let说它们的标题是: p>

  1。 伟大的芒果
2。 显示芒果
3。  King Mango 
  code>  pre> 
 
 

我使用这个学说来找到标题 p>

  $ repository-> findByTitle(“Mango”  “); 
  code>  pre> 
 
 

但我没有显示任何内容。 如果我完成标题搜索它像“国王芒果”。 它显示但不是带有MANGO字的列表。 p>

我使用了这个链接以供参考。 但如果您有更多关于此的文档。 我很高兴从中吸取教训。 p> div>

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();