Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

symfony - Easyadmin manyToMany relation not saving data in base

I have a ManyToMany relation between my ressource and my categories. But when i'm trying to create a new ressource with easyadmin, the choosed categories are not saved in database. Do you have any ideas?

Here is my ressource class


namespace AppEntity;

use AppRepositoryRessourceRepository;
use AppEntityUser;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass=RessourceRepository::class)
 */
class Ressource
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

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

    /**
     * @ORMColumn(type="text")
     */
    private $content;

    /**
     * @ORMColumn(type="string", length=500)
     */
    private $summary;

    /**
     * @ORMColumn(type="datetime")
     */
    private $createdAt;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $illustration;

    /**
     * @ORMManyToOne(targetEntity=User::class, inversedBy="ressources")
     * @ORMJoinColumn(nullable=false)
     */
    private $creator;

    /**
     * @ORMOneToMany(targetEntity=Favorite::class, mappedBy="ressources")
     */
    private $favorites;

    /**
     * @ORMManyToOne(targetEntity=RelationType::class, inversedBy="ressources")
     * @ORMJoinColumn(nullable=false)
     */
    private $relation_type;

    /**
     * @ORMManyToOne(targetEntity=RessourceType::class, inversedBy="ressources")
     * @ORMJoinColumn(nullable=false)
     */
    private $RessourceType;

    /**
     * @ORMManyToMany(targetEntity=Category::class, mappedBy="ressource")
     */
    private $categories;

    /**
     * @ORMOneToMany(targetEntity=Comment::class, mappedBy="ressource")
     */
    private $comments;

    /**
     * @ORMManyToOne(targetEntity=Status::class, inversedBy="ressource")
     * @ORMJoinColumn(nullable=false)
     */
    private $status;

    /**
     * @ORMColumn(type="boolean", options={"default":false})
     */
    private $isPublished;

    public function __construct()
    {
        $this->favorites = new ArrayCollection();
        $this->categories = new ArrayCollection();
        $this->comments = new ArrayCollection();
        $this->createdAt = new DateTime();
    }

    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 getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getSummary(): ?string
    {
        return $this->summary;
    }

    public function setSummary(string $summary): self
    {
        $this->summary = $summary;

        return $this;
    }

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

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

        return $this;
    }

    public function getIllustration(): ?string
    {
        return $this->illustration;
    }

    public function setIllustration(string $illustration): self
    {
        $this->illustration = $illustration;

        return $this;
    }

    public function getCreator(): ?User
    {
        return $this->creator;
    }

    public function setCreator(?User $creator): self
    {
        $this->creator = $creator;

        return $this;
    }

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

    public function addFavorite(Favorite $favorite): self
    {
        if (!$this->favorites->contains($favorite)) {
            $this->favorites[] = $favorite;
            $favorite->setRessources($this);
        }

        return $this;
    }

    public function removeFavorite(Favorite $favorite): self
    {
        if ($this->favorites->removeElement($favorite)) {
            // set the owning side to null (unless already changed)
            if ($favorite->getRessources() === $this) {
                $favorite->setRessources(null);
            }
        }

        return $this;
    }

    public function getRelationType(): ?RelationType
    {
        return $this->relation_type;
    }

    public function setRelationType(?RelationType $relation_type): self
    {
        $this->relation_type = $relation_type;

        return $this;
    }

    public function getRessourceType(): ?RessourceType
    {
        return $this->RessourceType;
    }

    public function setRessourceType(?RessourceType $RessourceType): self
    {
        $this->RessourceType = $RessourceType;

        return $this;
    }

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

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

        return $this;
    }

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

        return $this;
    }

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

    public function addComment(Comment $comment): self
    {
        if (!$this->comments->contains($comment)) {
            $this->comments[] = $comment;
            $comment->setRessource($this);
        }

        return $this;
    }

    public function removeComment(Comment $comment): self
    {
        if ($this->comments->removeElement($comment)) {
            // set the owning side to null (unless already changed)
            if ($comment->getRessource() === $this) {
                $comment->setRessource(null);
            }
        }

        return $this;
    }

    public function getStatus(): ?Status
    {
        return $this->status;
    }

    public function setStatus(?Status $status): self
    {
        $this->status = $status;

        return $this;
    }

    public function getIsPublished(): ?bool
    {
        return $this->isPublished;
    }

    public function setIsPublished(bool $isPublished): self
    {
        $this->isPublished = $isPublished;

        return $this;
    }
}

My category class


namespace AppEntity;

use AppRepositoryCategoryRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass=CategoryRepository::class)
 */
class Category
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $name;

    /**
     * @ORMManyToMany(targetEntity=Ressource::class, inversedBy="categories")
     */
    private $ressource;

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


    public function __construct()
    {
        $this->ressource = 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|Ressource[]
     */
    public function getRessource(): Collection
    {
        return $this->ressource;
    }

    public function addRessource(Ressource $ressource): self
    {
        if (!$this->ressource->contains($ressource)) {
            $this->ressource[] = $ressource;
        }

        return $this;
    }

    public function removeRessource(Ressource $ressource): self
    {
        $this->ressource->removeElement($ressource);

        return $this;
    }
}

The ressourceCrudController for easy admin


namespace AppControllerAdmin;

use AppEntityRessource;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldAssociationField;
use EasyCorpBundleEasyAdminBundleFieldBooleanField;
use EasyCorpBundleEasyAdminBundleFieldDateTimeField;
use EasyCorpBundleEasyAdminBundleFieldImageField;
use EasyCorpBundleEasyAdminBundleFieldTextareaField;
use EasyCorpBundleEasyAdminBundleFieldTextField;


class RessourceCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Ressource::class;
    }


    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('title', 'Titre'),
            TextField::new('summary', 'Résumé'),
            TextareaField::new('content', 'Contenu'),
            DateTimeField::new('createdAt', 'Date et heure de création'),
            ImageField::new('illustration', 'Image')
                ->setBasePath('uploads/')
                ->setUploadDir('public/uploads/')
                ->setUploadedFileNamePattern('[randomhash].[extension]')
                ->setRequired(false),
            AssociationField::new('creator', 'Créer par'),
            AssociationField::new('RessourceType', 'Type de ressource'),
            AssociationField::new('categories', 'Catégorie'),
            AssociationField::new('relation_type'),
            AssociationField::new('status'),
            BooleanField::new('isPublished'),

        ];
    }

}

When i'm completing the form to create a new ressource, i can select the categories, but after the validation, i have 0 in the categories column instead of the ones I chose. I dont have any idea about what is happening and i've find nothing on the internet. Thank you in advance for any tips, clues

question from:https://stackoverflow.com/questions/65900855/easyadmin-manytomany-relation-not-saving-data-in-base

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...