简体   繁体   English

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

[英]How to search a word in doctrine ORM?

I would like to know if this is possible in doctrine ORM symfony. 我想知道这是否可能在学说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. 它显示但不是带有MANGO字的列表。

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 要使用LIKE语句,您必须创建自己的存储库,如下所述: http//symfony.com/doc/current/doctrine/repository.html

Once you have your own repository class, create a method that uses LIKE in the query: 拥有自己的存储库类后,创建一个在查询中使用LIKE的方法:

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 . 要在where子句中使用like ,必须使用带有setParameter where条件。 query as below: 查询如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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