我正在研究一个项目,以便用户可以根据开始日期,结束日期,艺术家和城市来查找音乐演唱会。
当我提交搜索表单时,它没有显示结果。 希望您能帮助我解决这两个问题。
Ps:我有艺术家和城市实体的活动实体所有者。
控制器
class HomeController extends AbstractController
{
/**
* Display formsearch and events on index Page
*
* @Route("/", name="home")
* @param Request $request
*/
public function search(Request $request)
{
$events=[];
$searchData = new searchData();
$form = $this->createForm(SearchType::class, $searchData);
if ($request->isMethod('Post')) {
$form->handleRequest($request);
if ($form->isValid()) {
$events = $this->getDoctrine()->getRepository(Event::class)->findDataByCriteria($searchData);
}
} else {
$events = $this->getDoctrine()->getRepository(Event::class)->findLastThreeMonths();
}
return $this->render('home/index.html.twig',
['form' => $form->createView(), 'events' => $events]);
}
}
树枝(模板)
{% extends 'base.html.twig.' %}
{% block title %}Os Concerts{% endblock %}
{% block body %}
{#<!-- Form search --> #}
<div class="jumbotron text-center">
<h1>Concerts en France</h1>
<div class="jumbotron">
<div class="container">
<h3 class="text-left">Rechercher un évènement</h3>
{{ form_start(form) }}
<div class="form-row">
<div class="col">
{{ form_row(form.startDate) }}
</div>
<div class="col">
{{ form_row(form.endDate) }}
</div>
<div class="col">
{{ form_row(form.artist) }}
</div>
<div class="col">
{{ form_row(form.city) }}
</div>
<div class="col">
<button class="btn btn-secondary align-bottom">Rechercher</button>
</div>
</div>
{{ form_end(form) }}
</div>
</div>
</div>
{#<!-- search result display or events for the next 3 months --> #}
<div class="container" mt-4>
<table class="table table-striped" id="liste">
<thead>
<th scope="col">Où
</td>
<th scope="col">Quand
</td>
<th scope="col">Qui
</td>
</thead>
<tbody>
{% for event in events %}
<tr>
<td>{{ event.city.name }}</td>
<td>{{ event.date|date }}</td>
<td>{{ event.artist.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
存储库
public function findDataByCriteria(SearchData $searchData)
{
$qb = $this->createQueryBuilder('e');
$qb->select('e, a, c')
->leftJoin('e.artist', 'a')
->leftJoin('e.city', 'c');
if (($searchData->getStartDate() != null && $searchData->getEndDate()) != null) {
$qb->andWhere('e.date BETWEEN :startDate AND :endDate')
->setParameter('startDate', $searchData->getStartDate())
->setParameter('endDate', $searchData->getEndDate());
}
if ($searchData->getCity() != null) {
$qb->andWhere('c = :city')
->setParameter('city', $searchData->getCity());
}
if ($searchData->getArtist() != null) {
$qb->andWhere('a.name LIKE :artist')
->setParameter('artist', '%' . $searchData->getArtist() . '%');
}
return $qb
->getQuery()
->getResult();
}
实体SearchData
class SearchData
{
private $startDate;
private $endDate;
private $artist;
private $city;
public function getStartDate(): ?\DateTime
{
return $this->startDate;
}
public function setStartDate(\DateTime $startDate = null): void
{
$this->startDate = $startDate;
}
public function getEndDate(): ?\DateTime
{
return $this->endDate;
}
public function setEndDate(\DateTime $endDate = null): void
{
$this->endDate = $endDate;
}
public function getArtist(): ?string
{
return $this->artist;
}
public function setArtist(string $artist): void
{
$this->artist = $artist;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): void
{
$this->city = $city;
}
public function __toString()
{
return $this->name;
}
}
表单类型
class SearchType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('startDate', DateTimeType::class, [
'widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'html5' => false,
'attr' => ['class' => 'from'],
])
->add('endDate', DateTimeType::class, [
'widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'html5' => false,
'attr' => ['class' => 'to'],
])
->add('artist', TextType::class)
->add('city', EntityType::class, [
'class' => City::class,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => SearchData::class,
'csrf_protection' => false,
'translation_domain' => 'forms'
]);
}
}