Symfony创建搜索表单

时间:2018-10-23 15:19:21

标签: php forms symfony url

我是来找你的,因为我想填写搜索表格,但我挡住了控制器

我的问题是如何在get中制作表格?

我与您分享我的代码

我的控制器:

/**
 * @Route({"fr": "/recherche/search/?value={value}&region={region}&category={category}",
 *         "en": "/search/",
 *         "es": "/buscar/"}, name="search", methods="GET")
 * @param Request $request
 * @param string $value
 * @param string $region
 * @param string $category
 * @return Response
 * @throws \Exception
 */
public function searchAction(Request $request, string $value, string $region, string $category): Response
{
    $form = $this->createForm(SearchType::class);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid())
    {
        $value = $form->getData()->getTitle();
        $search = $this->getDoctrine()->getRepository(Advertisement::class)->findBySearch($value, $region, $category);

        return $this->render('search/result.html.twig', [
            'results' => $search
        ]);
    }
    return $this->render('search/search.html.twig', [
        'form' => $form->createView()
    ]);

}

在我的存储库中,我需要以下查询:

SELECT * FROM `advertisement` 
WHERE category_id = 7 
    AND region_id = 1 
    AND title LIKE '%iph%' 
    OR description LIKE 'test'

我的资料库:

/**
 * @return Advertisement|null
 * @param string $value
 * @param $region
 * @param $category
 * @throws \Exception
 */
public function findBySearch(string $value, $region, $category)

{
    $query = $this->createQueryBuilder('a')
    ->addSelect('a')
    ->where('a.category = :category')
    ->andWhere('a.region = :region')
    ->andWhere('a.title LIKE :value')
    ->orWhere('a.description LIKE :value')
    ->setParameter(':value', $value)
    ->setParameter(':region', $region)
    ->setParameter(':category', $category)
    ->getQuery();

    try {
        return $query->getResult();
    }
    catch(\Exception $e) {
        throw new \Exception('problème '. $e->getMessage(). $e->getFile());
    }
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您可以在创建表单方法(https://symfony.com/doc/current/form/action_method.html)时设置表单方法,尝试类似的操作

...    
public function searchAction(Request $request, string $value, string $region, string $category): Response
{
   $form = $this->createForm(SearchType::class, ['method' => Request::METHOD_GET]);
   $form->handleRequest($request);
        ...

答案 1 :(得分:0)

尝试一下,我认为地区和类别是古董

/**
 * @Route({"fr": "/recherche/search/?value={value}&region={region}&category={category}",
 *         "en": "/search/",
 *         "es": "/buscar/"}, name="search", methods="GET")
 * @param Request $request
 * @param string $value
 * @param string $region
 * @param string $category
 * @return Response
 * @throws \Exception
 */
public function searchAction(Request $request, string $value, Region $region, category $category): Response
{
    $form = $this->createForm(SearchType::class, null, [
        'method' => 'GET'
    ]);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid())
    {
        $value = $form->getData()->getTitle();
        $search = $this->getDoctrine()->getRepository(Advertisement::class)->findBySearch($value, $region, $category);

    return $this->render('search/result.html.twig', [
        'results' => $search
    ]);
}
return $this->render('search/search.html.twig', [
    'form' => $form->createView()
]);

}

public function findBySearch(string $value, Region $region, Category $category)

{
return $this->createQueryBuilder('a')
    ->where('a.category = :category')
    ->andWhere('a.region = :region')
    ->andWhere('a.title LIKE :value')
    ->orWhere('a.description LIKE :value')
    ->setParameters([
        'value' => $value,
        'region' => $region,
        'category' => $category
    ])
    ->getQuery()
    ->getResult();

    }