Я работаю над проектом, чтобы пользователи могли находить музыкальные концерты по дате начала, дате окончания, исполнителю и городу.
Когда я отправляю форму поиска, она не показывает результат,Я надеюсь, что вы можете помочь мне решить обе эти проблемы.
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]);
}
}
Twig (шаблон)
{% 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();
}
Данные поиска объекта
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'
]);
}
}