ux: [Live Component] The identifier id is missing for a query of ...

Hi, I try to test the LiveComponent from SymfonyUX and I have some trouble with my formComponent. I want to add a new object with this form so I think I made the same things like documentation but I have this exception when I try to update my field :

The identifier id is missing for a query of App\Entity\PreparePayslip

Here is my component code :

namespace App\Components;

use App\Entity\PreparePayslip;
use App\Form\PreparePayslipType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent('prepare_payslip_form')]
class PreparePayslipFormComponent extends AbstractController
{
    use ComponentWithFormTrait;
    use DefaultActionTrait;

    #[LiveProp(fieldName: 'data')]
    public ?PreparePayslip $preparePayslip = null;

    protected function instantiateForm(): FormInterface
    {
        return $this->createForm(PreparePayslipType::class, $this->preparePayslip);
    }
}

My template test for the component :

<div
        {{ attributes }}
        data-action="change->live#update"
>
    {{ form_start(form) }}
        {{ form_rest(form) }}
    {{ form_end(form) }}
</div>

I call this template with :

    {{ component('prepare_payslip_form', {
        preparePayslip: preparePayslip.id ? preparePayslip : null,
        form: form,
    }) }}

And finally my controller :

    #[Route('/preparation-fiche-paie/creer', name: 'prepare_payslip_create')]
    public function create(
        Request $request,
        EntityManagerInterface $entityManager,
        TranslatorInterface $translator,
        UserRepository $userRepository
    ): Response {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

        /** @var User $user */
        $user = $this->getUser();
        $preparePayslip = new PreparePayslip();
        $preparePayslip->setStatus(PreparePaySlipStatus::DRAFT);
        $preparePayslip->setUser($user);
        if ($request->query->get('impersonateUser')) {
            $impersonateUser = $userRepository->find(['id' => $request->query->get('impersonateUser')]);
            $preparePayslip->setUser($impersonateUser);
        }
        
        $form = $this->createForm(PreparePayslipType::class, $preparePayslip);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            if ($form->get('validate')->isClicked()) {
                $preparePayslip->setStatus(PreparePaySlipStatus::PENDING_SENDING);
            }
            $preparePayslip->setCreatedAt(new DateTime());

            $entityManager->persist($preparePayslip);
            $entityManager->flush();

            $this->addFlash('success', $translator->trans('flash.prepare_payslip.create.success'));

            return $this->redirectToRoute('dashboard');
        }

        return $this->renderForm('preparePayslip/create.html.twig', [
            'form' => $form,
            'preparePayslip' => $preparePayslip,
        ]);
    }

This form worked before I try to use LiveComponent. I think, I missed something but I can’t see where.

Thanks for your help 🙂

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Hey @Faulk13!

Sorry about the trouble! This is actually a bug that was already fixed - #295 - but hasn’t been released yet. I’m going to check into getting a tag out.

In the mean-time, you can update your composer.json to this: "symfony/ux-live-component": "2.x-dev", to use the latest version.

Cheers!