hey there I'm trying to do crud operation using Symfony and elastic search, crud is working fine but when I insert or delete data then elastic search data is not updating by self I need to run populate command every time. can you help with this?
here is my fos_elastica.yml file.
fos_elastica:
clients:
default: { url: '%env(ELASTICSEARCH_URL)%' }
indexes:
reply:
properties:
category:
type: object
properties:
title: ~
persistence:
driver: orm
model: App\Entity\Reply
and here is my controller.
<?php
namespace App\Controller\CP\Support\Replies;
use App\Controller\CP\AbstractCPController;
use App\Entity\Reply;
use App\Form\Filter\ReplyFilterType;
use App\Form\ReplyType;
use App\Repository\ReplyRepository;
use App\Security\Enum\AdminPermission;
use App\Security\Routing\DenyAccessUnlessGranted;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use FOS\ElasticaBundle\Finder\TransformedFinder;
use Elastica\Util;
use Throwable;
#[Route('/support/pre-defined-replies/reply')]
#[DenyAccessUnlessGranted(permission: AdminPermission::MANAGE_PRE_DEFINED_REPLIES_REPLY)]
class ReplyController extends AbstractCPController
{
private ReplyRepository $replyRepository;
public function __construct(ReplyRepository $replyRepository)
{
$this->replyRepository = $replyRepository;
}
#[Route('/', name: 'cp_support_pre_defined_replies_reply_index', methods: ['GET'])]
public function index(TransformedFinder $replyFinder, Request $request): Response
{
$searchTerm = '';
$filterForm = $this->createForm(ReplyFilterType::class);
$filterForm->handleRequest($request);
if ($filterForm->isSubmitted()) {
$category = $filterForm->get('category')->getData();
$searchTerm = $category->getName();
}
$search = Util::escapeTerm($searchTerm);
$results = $replyFinder->find($search);
return $this->render('support/pre_defined_replies/reply/index.html.twig', [
'replies' => $results,
'form' => $filterForm->createView(),
]);
}
#[Route('/new', name: 'cp_support_pre_defined_replies_reply_new', methods: ['GET', 'POST'])]
public function new(Request $request): Response
{
$reply = new Reply();
$form = $this->createForm(ReplyType::class, $reply);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->replyRepository->add($reply, true);
$this->addFlash('success', 'successfully created new reply.');
} catch (Throwable $throwable) {
$this->addFlash('danger', 'fail create new reply.');
}
return $this->redirectToRoute('cp_support_pre_defined_replies_reply_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('support/pre_defined_replies/reply/new.html.twig', [
'reply' => $reply,
'form' => $form,
]);
}
#[Route('/{id}/edit', name: 'cp_support_pre_defined_replies_reply_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Reply $reply): Response
{
$form = $this->createForm(ReplyType::class, $reply);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->replyRepository->add($reply, true);
$this->addFlash('success', 'successfully updated reply');
} catch (Throwable $throwable) {
$this->addFlash('danger', 'failed to update reply');
}
return $this->redirectToRoute('cp_support_pre_defined_replies_reply_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('support/pre_defined_replies/reply/edit.html.twig', [
'reply' => $reply,
'form' => $form,
]);
}
#[Route('/{id}/delete', name: 'cp_support_pre_defined_replies_reply_delete', methods: ['POST'])]
public function delete(Request $request, Reply $reply): Response
{
$csrfTokenReply = (string) $request->request->get('csrf');
$status = false;
$errorMessage = null;
if ($this->isCsrfTokenValid('reply', $csrfTokenReply)) {
try {
$this->replyRepository->remove($reply);
$status = true;
$this->addFlash('danger', 'category successfully deleted');
} catch (\Throwable $e) {
$errorMessage = 'An error occurred when deleting category from DB.';
}
} else {
$errorMessage = 'Invalid CSRF token';
}
return new JsonResponse(['status' => $status, 'errorMessage' => $errorMessage]);
}
}
we can use refresh indexes in elastic search for update data
first we need to bind new parameter in service.yaml file
then we can use simply like this in controller to refresh indexes
#[Route('/new', name: 'cp_support_pre_defined_replies_reply_new', methods: ['GET', 'POST'])] public function new(Request $request, Index $replyIndex): Response { $reply = new Reply(); $form = $this->createForm(ReplyType::class, $reply); $form->handleRequest($request);