Symfony Doctrine 搜索查询与父实体



>我正在使用 knp 分页器在 symfony 2.8 项目中工作所以我一直在尝试创建 costum 过滤器,问题是我正在使用它来搜索父实体,但我没有找到一种方法来做到这一点这是我的控制器:

public function OfferIndex(Request $request)
{
    $em    = $this->getDoctrine()->getManager();
    $queryBuilder = $em->getRepository('BackBundle:offer')->createQueryBuilder('o');
    if (isset($_REQUEST['offerKey'])) {
        $queryBuilder
            ->where('CONCAT(o.name,o.technologies) LIKE :title')
            ->setParameter('title', '%' . $_REQUEST['offerKey'] . '%');
    }
    $query = $queryBuilder->getQuery();
    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query, /* query NOT result */
        $request->query->getInt('page', 1)/*page number*/,
        2/*limit per page*/
    );
    return $this->render('FrontBundle:pages:Offre d'emploi et de stage .html.twig',array(
        'pagination' => $pagination
    ));
}

这个过滤器是我的树枝:

<form method="post" id="filtres" action="{{ path('offer/list') }}">
    <div class="filter with-reset-search">
        <input type="text" placeholder="Mots clés" id="offerKey" name="offerKey"/>
            <span class="reset-search">
                <svg version="1.1" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"
                                 xmlns:xlink="http://www.w3.org/1999/xlink"><g id="icons" fill="#0064b9"><path
                                            d="M14.8,12l3.6-3.6c0.8-0.8,0.8-2,0-2.8c-0.8-0.8-2-0.8-2.8,0L12,9.2L8.4,5.6c-0.8-0.8-2-0.8-2.8,0   c-0.8,0.8-0.8,2,0,2.8L9.2,12l-3.6,3.6c-0.8,0.8-0.8,2,0,2.8C6,18.8,6.5,19,7,19s1-0.2,1.4-0.6l3.6-3.6l3.6,3.6   C16,18.8,16.5,19,17,19s1-0.2,1.4-0.6c0.8-0.8,0.8-2,0-2.8L14.8,12z"
                                            id="exit"></path></g></svg>
    </span>
</div>

我想要的是将父实体添加到搜索查询中:

$queryBuilder
            ->where('CONCAT(o.name,o.technologies) LIKE :title')
            ->setParameter('title', '%' . $_REQUEST['offerKey'] . '%');

我努力在教义文档中找到这个 https://symfony.com/doc/2.8/doctrine.html但无济于事。

我找到了教义的解决方案,这不是最好的解决方案,但它目前有效:

public function OfferIndex(Request $request)
{
    //working solution
    $em    = $this->getDoctrine()->getManager();
    $queryBuilder = $em->createQueryBuilder();
    $queryBuilder->select(array('o', 't', 'r', 'c'))
        ->from('BackBundleEntityoffer', 'o')
        ->leftJoin('o.offerType', 't')
        ->leftJoin('o.offerRegion', 'r')
        ->leftJoin('o.offerCategory', 'c')
    ;
    if (isset($_REQUEST['offerKey'])) {
        $queryBuilder
            ->where('CONCAT(o.name,o.technologies,t.name,r.name,c.name) LIKE :search')
            ->setParameter('search', '%' . $_REQUEST['offerKey'] . '%');
    }
    $query = $queryBuilder->getQuery();
    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query, /* query NOT result */
        $request->query->getInt('page', 1)/*page number*/,
        2/*limit per page*/
    );
    return $this->render('FrontBundle:pages:Offre d'emploi et de stage .html.twig',array(
        'pagination' => $pagination
    ));
}

相关内容

  • 没有找到相关文章