首页 > 解决方案 > Symfony - 带有 entity.propety 过滤器的下拉列表

问题描述

我有 3 个实体,分别命名为 Answer、Skill 和 Jointure。

答案和技能通过多对一关系链接到关节。

我像这样在树枝中显示它们:

class HomeController extends AbstractController
{

    /**
     * @var JointureRepository
     */

    public function __construct(JointureRepository $repository, ObjectManager $em)
    {
        $this->repository = $repository;
        $this->em = $em;
    }

    /**
     * @Route("/", name="home")
     */
    public function index()
    {
        $JointureRepository = $this->getDoctrine()->getRepository(Jointure::class);
        $arrJointures = $JointureRepository->findAll();
        $this->em->flush();

        return $this->render('pages/home.html.twig', [
            'controller_name' => 'HomeController',
            'jointure' => $arrJointures,
        ]);
    }
}

在我的树枝视图中:

{% for object in jointure %}
    {% for skill in object.skills %}   
        {{skill.label}}  
    {% endfor %}
{% endfor %}

我创建了一个下拉按钮,其中列出了所有存在的 Skill.label 属性:

编辑:这是我的树枝按钮:

            <div class="form-group ">
            <select id="inputState " class="form-control">
              <option selected>Compétence</option>
              {% for object in jointure %}
              {% for skill in object.skills %}
              <option>{{skill.label}}</option>
              {% endfor %}
              {% endfor %}
            </select>
        </div>
      </div>
    <div class="col-md-2">
      <button type="submit" class="btn btn-primary btn-block">Search</button>
    </div>
  </div>

我想在模板的视图中显示/显示所有具有此相关技能标签的 answer.userEmail 。我必须使用 EntityType 吗?非常感谢

标签: symfonyfilterdropdown

解决方案


你应该开始使用 Symfony Forms。这是文档https://symfony.com/doc/current/forms.html。一开始并不那么简单,但绝对值得。然后你就可以使用EntityType https://symfony.com/doc/current/reference/forms/types/entity.html


推荐阅读