我在Users实体上遇到麻烦。
在我的网站上,用户可以填写自己的收藏集,以跟踪其魔术收集卡。每个用户只有一个集合。因此,我创建了一个表“ Collection”,该表映射到Users和Collection之间的OneToOne中。
此表集合已映射到ManyToOne
中名为“ CollectionContent”的另一个表。该表仅包含一个字段,数量。我创建此表是因为ManyToMany关联不允许additionalnals字段。表collectionContent然后映射到ManyToOne
中的Cards。 (模拟ManyToMany并附加一个字段。)问题来了
:我最初创建实体时出错,并在用户和Collection之间将其创建为ManyToOne。 (而不是OneToOne)现在,我想打电话给
$userCollectionId = $this->getUser()->getCollection();
应该返回用户集合ID,并且我收到一条错误消息:
Return value of App\Entity\Users::getCollection() must implement interface Doctrine\Common\Collections\Collection, instance of App\Entity\Collections returned
我想有两种可能性:
这是我的用户实体:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\UsersRepository")
* @UniqueEntity(
* fields={"email"},
* message="L'adresse mail est déjà utilisée."
* )
*/
class Users implements UserInterface
{
private const DEFAULT_IMAGE = "default.jpg";
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\Length(min="5", minMessage="Votre pseudo doit faire plus de 4 caractères")
*/
private $username;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(min="6", minMessage="Votre mot de passe doit faire plus de 5 caractères")
*/
private $password;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastName;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @ORM\OneToOne(targetEntity="App\Entity\Collections", mappedBy="users", orphanRemoval=true)
*/
private $collection;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Wishlists", mappedBy="users")
*/
private $wishlists;
/**
* @ORM\Column(type="datetime", options={"default" : "CURRENT_TIMESTAMP"})
*/
private $registeredAt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Comments", mappedBy="auteur", orphanRemoval=true)
*/
private $comments;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Comments", mappedBy="postedOnUser")
*/
private $commentsOnUser;
/**
* @ORM\Column(type="string", length=255, nullable=true, options={"default" : "default.jpg"})
*/
private $image;
public function __construct()
{
$this->collection = new ArrayCollection();
$this->wishlists = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->commentsOnUser = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getRoles(): array
{
$role = $this->roles;
// guarantee every user at least has ROLE_USER
$role[] = 'ROLE_USER';
return array_unique($role);
}
public function setRole(array $role): self
{
$this->roles = $role;
return $this;
}
/**
* @return Collection|collections[]
*/
public function getCollection(): Collection
{
return $this->collection;
}
public function addCollection(collections $collection): self
{
if (!$this->collection->contains($collection)) {
$this->collection[] = $collection;
$collection->setUsers($this);
}
return $this;
}
public function removeCollection(collections $collection): self
{
if ($this->collection->contains($collection)) {
$this->collection->removeElement($collection);
// set the owning side to null (unless already changed)
if ($collection->getUsers() === $this) {
$collection->setUsers(null);
}
}
return $this;
}
/**
* @return Collection|wishlists[]
*/
public function getWishlists(): Collection
{
return $this->wishlists;
}
public function addWishlist(wishlists $wishlist): self
{
if (!$this->wishlists->contains($wishlist)) {
$this->wishlists[] = $wishlist;
$wishlist->setUsers($this);
}
return $this;
}
public function removeWishlist(wishlists $wishlist): self
{
if ($this->wishlists->contains($wishlist)) {
$this->wishlists->removeElement($wishlist);
// set the owning side to null (unless already changed)
if ($wishlist->getUsers() === $this) {
$wishlist->setUsers(null);
}
}
return $this;
}
public function getRegisteredAt(): ?\DateTimeInterface
{
return $this->registeredAt;
}
public function setRegisteredAt(\DateTimeInterface $registeredAt): self
{
$this->registeredAt = $registeredAt;
return $this;
}
/**
* @return Collection|Comments[]
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comments $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setAuteur($this);
}
return $this;
}
public function removeComment(Comments $comment): self
{
if ($this->comments->contains($comment)) {
$this->comments->removeElement($comment);
// set the owning side to null (unless already changed)
if ($comment->getAuteur() === $this) {
$comment->setAuteur(null);
}
}
return $this;
}
/**
* @return Collection|Comments[]
*/
public function getCommentsOnUser(): Collection
{
return $this->commentsOnUser;
}
public function addCommentsOnUser(Comments $commentsOnUser): self
{
if (!$this->commentsOnUser->contains($commentsOnUser)) {
$this->commentsOnUser[] = $commentsOnUser;
$commentsOnUser->setPostedOnUser($this);
}
return $this;
}
public function removeCommentsOnUser(Comments $commentsOnUser): self
{
if ($this->commentsOnUser->contains($commentsOnUser)) {
$this->commentsOnUser->removeElement($commentsOnUser);
// set the owning side to null (unless already changed)
if ($commentsOnUser->getPostedOnUser() === $this) {
$commentsOnUser->setPostedOnUser(null);
}
}
return $this;
}
/**
* @inheritDoc
*/
public function getSalt()
{
// TODO: Implement getSalt() method.
}
/**
* @inheritDoc
*/
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
}
我在我的Users实体上遇到了麻烦。在我的网站上,用户可以填写自己的收藏集,以跟踪其魔术收集卡。每个用户只有一个集合。因此,我创建了...
我不知道不允许为实体“ Collection”命名,但这绝对是问题的根源。