Skip to content
Advertisement

Symfony 6 data access and transmission between multiple tables with format error [closed]

The project is a website written in symfony. The problem there is that there is a user and a consultant which is a separate table, but a user becomes the consultant, and when this consultant wants to give up his free time and I want to store it in a third table that listens for the interval name I have to fill in the consultant ID.

user_table:

<?php

namespace AppEntity;

use AppRepositoryUserRepository;
use DoctrineORMMapping as ORM;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyComponentSecurityCoreUserPasswordAuthenticatedUserInterface;
use SymfonyComponentSecurityCoreUserUserInterface;

/**
 * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
 */
#[ORMEntity(repositoryClass: UserRepository::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn(type: 'integer')]
    private $id;

    #[ORMColumn(type: 'string', length: 180, unique: true)]
    private $email;

    #[ORMColumn(type: 'json')]
    private $roles = [];

    #[ORMColumn(type: 'string')]
    private $password;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUserIdentifier(): string
    {
        return (string) $this->email;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see PasswordAuthenticatedUserInterface
     */
    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }
}

 

cons table:

<?php

namespace AppEntity;

use AppRepositoryConsRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: ConsRepository::class)]
class Cons
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn(type: 'integer')]
    private $id;

    #[ORMOneToOne(targetEntity: User::class, cascade: ['persist', 'remove'])]
    private $user;

    #[ORMOneToMany(mappedBy: 'cons_id', targetEntity: Intervall::class)]
    private $intervalls;

    public function __construct()
    {
        $this->intervalls = new ArrayCollection();
    }


    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUserId(): ?User
    {
        return $this->user;
    }

    public function setUserId(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

    /**
     * @return Collection|Intervall[]
     */
    public function getIntervalls(): Collection
    {
        return $this->intervalls;
    }

    public function addIntervall(Intervall $intervall): self
    {
        if (!$this->intervalls->contains($intervall)) {
            $this->intervalls[] = $intervall;
            $intervall->setConsId($this);
        }

        return $this;
    }

    public function removeIntervall(Intervall $intervall): self
    {
        if ($this->intervalls->removeElement($intervall)) {
            // set the owning side to null (unless already changed)
            if ($intervall->getConsId() === $this) {
                $intervall->setConsId(null);
            }
        }

        return $this;
    }
}

intervall table:

<?php

namespace AppEntity;

use AppRepositoryIntervallRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: IntervallRepository::class)]
class Intervall
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn(type: 'integer')]
    private $id;

    #[ORMColumn(type: 'datetime')]
    private $start;

    #[ORMColumn(type: 'datetime')]
    private $end;

    #[ORMColumn(type: 'boolean')]
    private $more;

    #[ORMColumn(type: 'time')]
    private $cons_time;

    #[ORMColumn(type: 'time')]
    private $free_time;

    #[ORMManyToOne(targetEntity: Cons::class, inversedBy: 'intervalls')]
    private $cons;

    public function __construct()
    {
        $this->cons = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getStart(): ?DateTimeInterface
    {
        return $this->start;
    }

    public function setStart(DateTimeInterface $start): self
    {
        $this->start = $start;

        return $this;
    }

    public function getEnd(): ?DateTimeInterface
    {
        return $this->end;
    }

    public function setEnd(DateTimeInterface $end): self
    {
        $this->end = $end;

        return $this;
    }


    public function getMore(): ?bool
    {
        return $this->more;
    }

    public function setMore(bool $more): self
    {
        $this->more = $more;

        return $this;
    }

    public function getConsTime(): ?DateTimeInterface
    {
        return $this->cons_time;
    }

    public function setConsTime(DateTimeInterface $cons_time): self
    {
        $this->cons_time = $cons_time;

        return $this;
    }

    public function getFreeTime(): ?DateTimeInterface
    {
        return $this->free_time;
    }

    public function setFreeTime(DateTimeInterface $free_time): self
    {
        $this->free_time = $free_time;

        return $this;
    }

    public function getConsId(): ?Cons
    {
        return $this->cons;
    }

    public function setConsId(?Cons $cons): self
    {
        $this->cons = $cons;

        return $this;
    }
}

intervall wrong code slise:

#[Route('/new', name: 'intervall_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager): Response
    {


        $user_id = $this->getUser()->getId();
        $cnsuseridrepo = $entityManager->getRepository(Cons::class);
        $cnsuserid = $cnsuseridrepo->findOneBy(["user"=>$user_id]);


        $intervall = new Intervall();
        $intervall->setConsId(($cnsuserid->getId());
        $form = $this->createForm(IntervallType::class, $intervall);
        $form->handleRequest($request);


        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->persist($intervall);
            $entityManager->flush();

            return $this->redirectToRoute('intervall_index', [], Response::HTTP_SEE_OTHER);
        }

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

that would be the mistake I see:

enter image description here

Advertisement

Answer

Seems like setter method allows only object of Cons or null value, so you have to set it as object.

$intervall = new Intervall();
$intervall->setConsId($cnsuserid);
$form = $this->createForm(IntervallType::class, $intervall);
$form->handleRequest($request);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement