Symfony搜索,参数不发送

时间:2018-05-05 08:48:33

标签: php symfony search model-view-controller

我的实体:广告是与地址的OneToMany关系,而地址是与城市的OneToMany。在City我有大约200个城市,用户可以从EntityType中选择1个城市。我试图做搜索引擎(?),用户可以按城市过滤广告。我不确定我是怎么做得好的,我的searchController有一个动作:

/**
 * @Route("/search/city", name="search_city")
 *
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function searchCityAction(Request $request)
{
    $search = new City();

    $form = $this->createForm(CityType::class, $search)
        ->add('submit', SubmitType::class, ["label" => "Szukaj"]);


    $entityManager = $this->getDoctrine()->getManager();


    if($request->isMethod(Request::METHOD_POST))
    {
        $form->handleRequest($request);

        $selectedCity = ($_REQUEST['city']['city']);

        $address = $entityManager->getRepository(Address::class)->findBy(["city" => $selectedCity]);
        $searched = $entityManager->getRepository(Advert::class)->findBy(["address" => $address]);

        return $this->render("Search/searched.html.twig", ["searched" => $searched]);
    }


    return $this->render("Search/index.html.twig", ["form" => $form->createView()]);
}

index.html.twig:

{% extends 'base.html.twig' %}

{% block body %}
    {{ form(form) }}
{% endblock %}

searched.html.twig:

{% extends 'base.html.twig' %}

{% block body %}
    {% for search in searched %}
        {{ search.title }}
    {% endfor %}
{% endblock %}

在$ selectedCity的控制器中,我获得了好城市的身份。但后来某种方式缺少参数。在广告实体中,我得到了变量$ address,在地址中我得到了变量$ city,我试图从这两行的DB中取出这个:

$address = $entityManager->getRepository(Address::class)->findBy(["city" => $selectedCity]);
$searched = $entityManager->getRepository(Advert::class)->findBy(["address" => $address]);

但无论如何它不能正常工作,而且我觉得我已经很多了,我迷路了

修改

我试图做第一个解决方案,但它没有用。我改变了我的代码:

if($request->isMethod(Request::METHOD_POST))
    {
        $form->handleRequest($request);

        $selectedCity = ($_REQUEST['city']['city']);

        $address = $entityManager->getRepository(Address::class)->findBy(["city" => $selectedCity]);
        $addressIds = $address->map(function($single_address) {
            return $single_address->id;
        });
        $searched = $entityManager->getRepository(Advert::class)->findBy(["address" => $addressIds]);

        return $this->render("Search/searched.html.twig", ["searched" => $searched]);
    }
  

方法" map"在阵列中找不到......

网站错误:

  

调用数组

上的成员函数map()

这里是我的实体的定义变量代码:

广告类:

/**
 * @var Address
 *
 * @ORM\ManyToOne(targetEntity="Address", inversedBy="adverts", cascade={"persist"})
 */
private $address;

地址类:

/**
 * @var Advert[]|ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="Advert", mappedBy="address")
 * @ORM\JoinColumn(name="address_id", referencedColumnName="id")
 */
private $adverts;

/**
 * @var City
 *
 * @ORM\ManyToOne(targetEntity="City", inversedBy="addresses")
 */
private $city;

城市课程:

/**
 * @var Address[]|ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="Address", mappedBy="city")
 * @ORM\JoinColumn(name="city_id", referencedColumnName="id")
 */
private $addresses;

1 个答案:

答案 0 :(得分:1)

使用Doctrine Entity Manager findBy方法从数据库查询实体时,数据将作为ArrayCollection实体对象返回。因此,您不能将它们直接作为另一个findBy函数的参数传递。

在帖子$address中,您发布的是Address个实例的集合。您可以从集合中提取ID数组,并使用它们查询Advert。例如:

$searched = $entityManager->getRepository(Advert::class)->findBy(["address" => $addressIds]);

假设Address类有一个名为id的默认密钥属性,您可以使用ArrayCollection.map函数提取地址ID列表:

$addressIds = $addresses->map(function($single_address) {
   return $single_address->id;
})

这种方法可以实现您的目标。但不是唯一且实际上更好的方式。

基本上,如果您在bi-directionalAddress实体之间建立了Advert(2种方式)关系。您只需使用以下命令访问链接到Advert实体的Address实体列表:

$address->adverts

我认为你adverts是映射到Address类的Advert类的属性。

可以在此处找到更多关于与Doctrine和Symfony查询和建立关系的信息:https://symfony.com/doc/current/doctrine.html