对于我的项目,我需要将一个或多个待办事项属性给一个或多个用户。
我的实体:
待办事项
namespace App\Entity;
use App\Repository\TodosRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TodosRepository::class)]
class Todos
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $color = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $description = null;
#[ORM\ManyToMany(targetEntity: Users::class, inversedBy: 'todo_id')]
private Collection $owner;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $date_created = null;
public function __construct()
{
$this->owner = new ArrayCollection();
}
(...)
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection<int, Users>
*/
public function getOwner(): Collection
{
return $this->owner;
}
public function addOwner(Users $owner): self
{
if (!$this->owner->contains($owner)) {
$this->owner->add($owner);
}
return $this;
}
public function removeOwner(Users $owner): self
{
$this->owner->removeElement($owner);
return $this;
}
(...)
}
用户/所有者
namespace App\Entity;
use App\Repository\UsersRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: UsersRepository::class)]
class Users
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\ManyToMany(targetEntity: Todos::class, mappedBy: 'owner')]
private Collection $todo_id;
public function __construct()
{
$this->todo_id = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection<int, Todos>
*/
public function getTodoId(): Collection
{
return $this->todo_id;
}
public function addTodoId(Todos $todoId): self
{
if (!$this->todo_id->contains($todoId)) {
$this->todo_id->add($todoId);
$todoId->addOwner($this);
}
return $this;
}
public function removeTodoId(Todos $todoId): self
{
if ($this->todo_id->removeElement($todoId)) {
$todoId->removeOwner($this);
}
return $this;
}
}
我的控制器:
if ($checkItems) {
$user = $usersRepo->findBy(array("id" => $idUser));
$todo = $todosRepo->findBy(array("id" => $idTodo));
$newTodo = new Todos();
$newOwner = new Users();
$em = $doctrine->getManager();
$newTodo->addOwner($user[0]);
$newOwner->addTodoId($todo[0]);
$em->persist($newTodo);
$em->persist($newOwner);
$em->flush();
return $this->redirectToRoute("app_todos");
}
但我有一个错误消息:
执行查询时出现异常错误:数据库状态[23000]:
完整性约束冲突:1048字段'name'不能为空(null)
我不明白为什么,因为我不需要创建新的***TODO***或新的***USERS***,只需添加关系即可。
任何帮助都是受欢迎的!
1条答案
按热度按时间uxhixvfz1#
发生约束冲突的原因是您向数据库中添加了一个新用户,但没有所有必需字段。在
users
实体中,name
字段为not
nullable
。如果将名称设为
nullable
,则不会再发生此错误。