Doctrine orm Symfony/条令:多对一实体的问题
我的用户有问题 在我的网站上,用户可以填写他们的收藏,以跟踪他们的魔法收集卡。 每个用户只有一个集合。因此,我创建了一个表“Collection”,在OneTONE中映射到用户和Collection之间 此表集合映射到manytone中名为“CollectionContent”的另一个表。 此表仅包含一个字段“数量”。我创建此表是因为多人关联不允许添加字段 然后,表collectionContent映射到多个One中的卡。(使用附加字段模拟多人。) 问题来了:我最初创建实体时犯了一个错误,在用户和集合之间创建了多个实体。(代替OneToOne) 现在,我想打电话给你Doctrine orm Symfony/条令:多对一实体的问题,doctrine-orm,doctrine,many-to-many,entity,symfony4,Doctrine Orm,Doctrine,Many To Many,Entity,Symfony4,我的用户有问题 在我的网站上,用户可以填写他们的收藏,以跟踪他们的魔法收集卡。 每个用户只有一个集合。因此,我创建了一个表“Collection”,在OneTONE中映射到用户和Collection之间 此表集合映射到manytone中名为“CollectionContent”的另一个表。 此表仅包含一个字段“数量”。我创建此表是因为多人关联不允许添加字段 然后,表collectionContent映射到多个One中的卡。(使用附加字段模拟多人。) 问题来了:我最初创建实体时犯了一个错误,在用户
$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
我想有两种可能性:
- 我不能将我的实体命名为“集合”?是保留字还是什么
- 从用户那里获取集合的方法不再好了,因为我将ManyToOne更改为OneToOne,并且我必须删除一些内容,但我不知道具体是什么
<?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;
}
}
我不知道不允许将实体命名为“集合”,但这肯定是问题的根源
您需要getCollection()
在代码中返回类型Collection
:
/**
* @return Collection|collections[]
*/
public function getCollection(): Collection
{
return $this->collection;
}
但是,您的文件顶部有一个use语句:
use Doctrine\Common\Collections\Collection;
这会产生您所看到的错误。我建议将您的收藏
实体重命名为不太容易混淆的名称,如CardCollection
。这将是解决整个问题的简单方法