Symfony 6 多表间数据访问和传输格式错误

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

该项目是一个用symfony编写的网站。问题是 有一个 user 和一个 consultant 这是一个单独的 table,但是一个用户 成为顾问,当这个顾问想放弃他的 空闲时间,我想将它存储在第三个 table 中,用于监听 区间名称我要填顾问ID

user_table:

<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
 */
#[ORM\Entity(repositoryClass: UserRepository::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

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

    #[ORM\Column(type: 'json')]
    private $roles = [];

    #[ORM\Column(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;
    }
}

 

缺点table:

<?php

namespace App\Entity;

use App\Repository\ConsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ConsRepository::class)]
class Cons
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

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

    #[ORM\OneToMany(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;
    }
}

间隔 table:

<?php

namespace App\Entity;

use App\Repository\IntervallRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: IntervallRepository::class)]
class Intervall
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(type: 'datetime')]
    private $start;

    #[ORM\Column(type: 'datetime')]
    private $end;

    #[ORM\Column(type: 'boolean')]
    private $more;

    #[ORM\Column(type: 'time')]
    private $cons_time;

    #[ORM\Column(type: 'time')]
    private $free_time;

    #[ORM\ManyToOne(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;
    }
}

间隔错误代码滑动:

#[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,
        ]);
    }

那是我看到的错误:

好像setter方法只允许Cons的对象或空值,所以你必须将它设置为对象。

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