Symfony mongo。如何搜索文件的名称?

时间:2015-02-18 21:04:56

标签: mongodb symfony search service repository

我有搜索服务的表单操作

<div class="search">
        <form action="{{ path('app_song_search') }}" metod="POST" class="form-search">
            <input type="text" name="search" class="input-medium search-query" />
            <input type="submit" class="btn" value="Search"/>
        </form>
    </div>

DB - Mongo,在文档歌曲中搜索字段$ name 我创建服务

class Search {
protected $repository;

public function __construct(SongRepository $repository)
{
    $this->repository = $repository;
}

public function search($string)
{
    return $this->repository->getIdArrayByName($string);
}
}

我有行动

public function searchAction(Request $request)
{
    $searcher = $this->get('searcher');
    $result = $searcher->search($request->get('search'));
    $repository = $this->get('doctrine_mongodb')->getRepository('AppBundle:Song');
    $query = $repository->createQueryBuilder('m')
        ->where('m.id IN (:ids)')
        ->setParameter('ids', $result)
        ->getQuery();
    $song = $query->getResult();

    if (!$song){
        throw $this->createNotFoundException('Opss, dont search');
    }

    return $this->render('AppBundle::serchSong.html.twig', array('song' => $song));
}

和songRepository

function getIdArrayByName($name)
{
    return $this->getDocumentManager()->createQueryBuilder($this->findByName($name))
        ->setQueryArray('name', '%'.$name.'%');
}

注意:未定义的偏移:0。我做错了什么?

0 个答案:

没有答案