Symfony-具有entity.propriety过滤器的下拉列表

时间:2019-02-27 13:40:33

标签: symfony filter dropdown

我有3个实体,分别名为答案,技能和接合。

答案和技能通过ManyToOne关系链接到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吗?非常感谢

2 个答案:

答案 0 :(得分:3)

您应该开始使用Symfony表单。这是文档https://symfony.com/doc/current/forms.html。一开始并不是那么简单,但是绝对值得。然后,您将可以使用https://codepen.io/FedorT/pen/wOapyy https://symfony.com/doc/current/reference/forms/types/entity.html

答案 1 :(得分:0)

感谢弗拉德里亚(VladRia)的建议,但我发现了另一种方法,我解释说: 您如何看待这种方式?最佳做法是什么?我猜这有点古怪/奇怪吗?

这是我的控制方:

/**
 * @Route("/recherche/{skillId}/{levelId}", name="recherche")
 */
public function recherche(Skill $skillId=null, Level $levelId=null): Response
{
    $filtre = $this->init();
    $answers = $filtre['answers'];
    $skills = $filtre['skills'];
    $levels = $filtre['levels'];
    $responses = null;

    $jointureRepository= $this->getDoctrine()->getRepository(Jointure::class);
    $jointures = $jointureRepository->findBySkillAndLevel($skillId,$levelId);
    $this->em->flush();

    foreach($jointures as $uneJointure){
    $responses[]=$uneJointure->getAnswer();
    }

    return $this->render('pages/home.html.twig', [
        'answers' => $answers,
        'skills' => $skills,
        'levels' => $levels,
        'responses' => $responses
    ]);
}

private function init(){

    $LevelRepository = $this->getDoctrine()->getRepository(Level::class);
    $levels = $LevelRepository->findAll();

    $SkillRepository = $this->getDoctrine()->getRepository(Skill::class);
    $skills = $SkillRepository->findAll();

    $AnswerRepository = $this->getDoctrine()->getRepository(Answer::class);
    $answers = $AnswerRepository->findAll();

    $this->em->flush();

    return ([
        'levels' => $levels,
        'skills' => $skills,
        'answers' => $answers,
    ]);
}

我的私有函数init()很奇怪,您能给我什么建议?

我的JointureRepository包含findBySkillAndLevel()querybuilder:

/ **  * @return Jointure []  * / 公共函数findBySkillAndLevel($ skillId,$ levelId):数组 {

$query = $this->createQueryBuilder('j')
    //->join('j.answer', 'a')
    ->join('j.skill', 's')
    ->join('j.level', 'l')
    //->where('a.id = :j.answerId')
    ->andWhere('s.id = :skillId')
    ->andWhere('l.id = :levelId')
    ->setParameter('skillId', $skillId)
    ->setParameter('levelId', $levelId)
    ->getQuery()
    ->getResult();

return $query;

}

我的树枝视图:(我可以在Symfony的最佳实践中将此JS放在哪里?)

<div class="container-fluid bg-light ">
  <div class="row justify-content-center">
      <div class="col-md-2 pt-3">
          <div class="form-group">
              <select id="inputState" class="form-control">
                <option selected>Compétences</option>
                {% for skill in skills %}
                <option value="{{skill.id}}">{{skill.label}}</option>
                {% endfor %}
              </select>
          </div>
      </div>
      <div class="col-md-2 pt-3">
        <div class="form-group">
          <select id="inputState2" class="form-control">
            <option selected>Maîtrise</option>
            {% for level in levels %}
            <option value="{{level.id}}">{{level.label}}</option>
            {% endfor %}
          </select>
        </div>
      </div>
      <div class="col-md-2 pt-3">
        <button type="button" onclick="search('{{ path('recherche') }}')" class="btn btn-info">Rechercher</button>
      </div>
  </div>
</div>

<div class="container">
  {% if responses is not null %}
    <h3>{{ responses|length }} profils correspondent aux critères séléctionnés :</h3>
  {% elseif responses|length == 0 %}
    <h3>Pas de résultats pour ces critères..</h3>
  {% else %}
    <h3>Faites votre recherche</h3>
  {% endif %}
  {% for rep in responses %}
  <div class="card">
    <div class="card-header">Email : {{ rep.UserEmail }}</div>
    <div class="card-body">Compétences :<br>
      {% for j in rep.jointures %}
      <p>{{j.level.label}} la compétence {{ j.skill.label }}</p>
      {% endfor %}
    </div>
    <div class="card-footer">Formulaire envoyé le : {{ rep.Date|date("d/m/Y") }} </div> 
  </div><br>
  {% endfor %}

</div>

<script>
function search(path){
var e = document.getElementById("inputState");
var value = e.options[e.selectedIndex].value;
var text = e.options[e.selectedIndex].text;
var l = document.getElementById("inputState2");
var test = l.options[l.selectedIndex].value;
path = path + '/' + value + '/' + test;
document.location.href = path;
}
</script>

我的问题是:为了尊重Symfony的最佳做法,我该如何做得更好?