Search code examples
phpsymfonysymfony4

Symfony - Expected value of type '' got “string” instead


I am trying to pass id from one formation table to ticket table but I can't' pass this error..

Expected value of type "App\Entity\Formation" for association field "App\Entity\Ticket#$formation", got "string" instead.

Formation Entity :

/**
 * @ORM\Entity(repositoryClass="App\Repository\FormationRepository")
 */
class Formation
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="array", nullable=true)
     */
    private $formations = [];

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private $customerName;
...

Ticket Entity :

/**
 * @ORM\MappedSuperclass()
 */
class Ticket implements TicketInterface
{
    use Timestampable;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;


    /**
     * @ORM\ManyToOne(targetEntity="Symfony\Component\Security\Core\User\UserInterface")
     * @ORM\JoinColumn(name="author", referencedColumnName="id", nullable=true)
     */
    private $author;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Formation")
     * @ORM\JoinColumn(nullable=false)
     */
    private $formation;
...

My ticketController with addTicket() :

public function addTicket(Request $request, TicketManager $ticketManager): Response
{
    $ticket = $ticketManager->newClass();
    $user = $this->getUser();

    $formationId = $user->getFormationId()->getId();
    $ticketForm = $this->createForm(TicketForm::class, $ticket);
    $ticketForm->handleRequest($request);

    if ($ticketForm->isSubmitted() && $ticketForm->isValid()) {
        $ticketManager->createTicket($user, $ticket, $formationId);
        $this->addFlash('success', 'Le ticket est en ligne !');

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

    return $this->render($this->ticketingTemplates['new'], [
        'form' => $ticketForm->createView(),
    ]);
}

And my ticketManager with createTicket() :

/**
 * @param UserInterface $user
 * @param TicketInterface $ticket
 * @param string $formationId
 * @throws \Doctrine\ORM\ORMException
 * @throws \Doctrine\ORM\OptimisticLockException
 */
public function createTicket(UserInterface $user, TicketInterface $ticket, string $formationId)
{
    $status = $this->ticketStatusManager->getOpenStatus();
    $ticket->setStatus($status)->setFormationId($formationId)->setAuthor($user)->setPublic(false);

    if (!$this->isTicketRestrictionEnabled()) {
        $ticket->setPublicAt(new \DateTime())->setPublic(true);
    }

    $this->persistAndFlush($ticket);
}

I want that when adding a ticket, I save the "formationId" in the ticket table. The rest works well, there is only the registration of the formationId that does not work

EDIT :

Definition of setFormationId() in Ticket Entity :

public function setFormation($formation): self
{
    $this->formation = $formation;
    return $this;
}

My ticketController with addTicket() :

public function addTicket(Request $request, TicketManager $ticketManager): Response
{
    $ticket = $ticketManager->newClass();
    $user = $this->getUser();

    $formationId = $user->getFormationId();
    $ticketForm = $this->createForm(TicketForm::class, $ticket);
    $ticketForm->handleRequest($request);

    if ($ticketForm->isSubmitted() && $ticketForm->isValid()) {
        $ticketManager->createTicket($user, $ticket, $formationId);
        $this->addFlash('success', 'Le ticket est en ligne !');

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

    return $this->render($this->ticketingTemplates['new'], [
        'form' => $ticketForm->createView(),
    ]);
}

Same error


Solution

  • /**
     * @param UserInterface $user
     * @param TicketInterface $ticket
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function createTicket(UserInterface $user, TicketInterface $ticket, Formation $formation)
    {
        $status = $this->ticketStatusManager->getOpenStatus();
        $ticket->setStatus($status)->setFormation($formation)->setAuthor($user)->setPublic(false);
    
        if (!$this->isTicketRestrictionEnabled()) {
            $ticket->setPublicAt(new \DateTime())->setPublic(true);
        }
    
        $this->persistAndFlush($ticket);
    }