简体   繁体   English

Symfony 5 :Relation ManyToMany 不保存

[英]Symfony 5 :Relation ManyToMany do not save

in my project i create 2 entities :Projects & category.They are a ManyToMany relation MySql shema im using Easy-admin bundle trying to Manage my database but the problem is when i try to add a category to a project it will not save, it work when i try to add a project to a category but i don't know why it fails in the other way .在我的项目中,我创建了 2 个实体:项目和类别。它们是多对多关系MySql shema,我使用 Easy-admin 包尝试管理我的数据库,但问题是当我尝试向项目添加类别时,它不会保存,它当我尝试将项目添加到类别时工作,但我不知道为什么它会以另一种方式失败。 thanks for help :)感谢帮助 :)

Category类别

  <?php

   namespace App\Entity;

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

  /**
   * @ORM\Entity(repositoryClass=CategoryRepository::class)
   * @ORM\Table(name="category")
   */
   class Category
   {
   /**
    * @ORM\Id
    * @ORM\GeneratedValue
    * @ORM\Column(type="integer")
    */
     private $id;

     /**
      * @ORM\Column(type="string", length=255)
      */
 private $title;

  /**
   * @ORM\Column(type="text")
   */
     private $body;

 /**
  * @ORM\Column(type="string", length=255)
  */
 private $image;

/**
 * @ORM\Column(type="datetime")
 */
private $createdAt;

/**
 * @ORM\ManyToMany(targetEntity=Projects::class, inversedBy="category",cascade={"persist"})
 * 
 */
private $projects;

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

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

public function getTitle(): ?string
{
    return $this->title;
}

public function setTitle(string $title): self
{
    $this->title = $title;

    return $this;
}

public function getBody(): ?string
{
    return $this->body;
}

public function setBody(string $body): self
{
    $this->body = $body;

    return $this;
}

public function getImage(): ?string
{
    return $this->image;
}

public function setImage(string $image): self
{
    $this->image = $image;

    return $this;
}

public function getCreatedAt(): ?\DateTimeInterface
{
    return $this->createdAt;
}

public function setCreatedAt(\DateTimeInterface $createdAt): self
{
    $this->createdAt = $createdAt;

    return $this;
}

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

public function addProjects(projects  $project): self
{
    if (!$this->projects->contains($project)) {
        $this->projects[] = $project;
        $project->addCategory($this);
    }

    return $this;
}

public function removeProjects(projects $project): self
{
    if ($this->projects->contains($project)) {
        $this->projects->removeElement($project);
        $project->removeCategory($this);
    }

    return $this;
}


public function __toString()
{
   return $this->title;
}

}

projects项目

<?php

 namespace App\Entity;

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

/**
 * @ORM\Entity(repositoryClass=ProjectsRepository::class)
 * @ORM\Table(name="projects")
 */
 class Projects
 {
/**
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $title;

/**
 *
 * @ORM\Column(type="text")
 */
private $body;

/**
 * @ORM\Column(type="string", length=255)
 */
private $image;

/**
 * @ORM\Column(type="datetime")
 */
private $createdAt;

/**
 * @ORM\ManyToMany(targetEntity=Category::class, mappedBy="projects",cascade={"persist"})
 */
private $category;

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

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

public function getTitle(): ?string
{
    return $this->title;
}

public function setTitle(string $title): self
{
    $this->title = $title;

    return $this;
}

public function getBody(): ?string
{
    return $this->body;
}

public function setBody(string $body): self
{
    $this->body = $body;

    return $this;
}

public function getImage(): ?string
{
    return $this->image;
}

public function setImage(string $image): self
{
    $this->image = $image;

    return $this;
}

public function getCreatedAt(): ?\DateTimeInterface
{
    return $this->createdAt;
}

public function setCreatedAt(\DateTimeInterface $createdAt): self
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * @return Collection|category
 */
public function getCategory(): Collection
{
    return $this->category;
}

public function addCategory(category $category): self
{
    if (!$this->category->contains($category)) {
         $this->category[] = $category;
         $category->addProjects($this);
    }

    return $this;
}

public function removeCategory(Category $category): self
{
    if ($this->category->contains($category)) {
        $this->category->removeElement($category);
        $category->removeProjects($this);
        
    }

    return $this;
}
public function __toString()
{
   return $this->title;
}


}

I suspect, that the adders and removers are never called.我怀疑,从未调用过加法器和移除器。 In the case of Category::getProjects you return the collection, which, when changed, will lead to updates in the database, since it's the owning side of the relation.在 Category::getProjects 的情况下,您返回集合,当更改时,将导致数据库更新,因为它是关系的拥有方。

On the other side however, you got Projects::getCategory, which also returns the collection, which probably is modified, but the changes are not propagated to the database at all, since it's the inverse side.然而,另一方面,您得到了 Projects::getCategory,它也返回可能被修改的集合,但更改根本不会传播到数据库,因为它是相反的一面。

To fix this, I presume you have to fix your pluralization:为了解决这个问题,我认为你必须修复你的复数形式:

Category's property should be called $projects, the getter should be called getProjects and imho return $this->projects->toArray(); Category 的属性应该叫做 $projects,getter 应该叫做getProjects并且 imho return $this->projects->toArray(); (the implementation details should be hidden), the adder should be named addProject (note the missing s) and the remover removeProject . (应该隐藏实现细节),加法器应该命名为addProject (注意缺少的 s)和移除器removeProject Symfony will handle the different pluralizations and understand which methods to call. Symfony 将处理不同的复数形式并了解调用哪些方法。

the class should be called Project (no plural s) and similarly the getters, adders and removers have their names getCategories , addCategory and removeCategory .该类应该被称为 Project(没有复数),类似地,getter、 addCategory和 removers 有它们的名字getCategoriesaddCategoryremoveCategory again, in the getter return $this->categories->toArray().同样,在 getter 中返回 $this->categories->toArray()。 providing the collection might lead to the bundle editing the collection directly, especially if the adders and removers are named weirdly.提供集合可能会导致包直接编辑集合,特别是如果加法器和移除器的命名很奇怪。

After these changes (adapting the @ManyToMany annotations too of course), it should work (tm).在这些更改之后(当然也要调整 @ManyToMany 注释),它应该可以工作(tm)。

it worked the mapping was false it should be like this in category它起作用了映射是错误的它应该像这样在类别中

@ORM\ManyToMany(targetEntity=Projects::class, mappedBy="categories")

and like this in projects在项目中像这样

@ORM\ManyToMany(targetEntity=Category::class, inversedBy="projects")

then i droped the db and update the schema ... plus the fix of the pluralization thnx a lot <3 <3然后我删除了数据库并更新了架构......加上复数的修复很多 <3 <3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM