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
171 views
in Technique[技术] by (71.8m points)

php - ManyToMany My form update the current record instead of adding a new one

I have two entities (List and Gift) with a ManytoMany relationship. I had also generated crud with symfony console make:crud.

However, in my mysql database, the entity's generation had created a table list_gift with an integer attribute named gift_id. As it's an integer, I only can have one gift per list (when I tried to add another gift in a list it updates the current gift instead of adding a new one).

In my List.php, I have an attribute Gift and its type is a Collection.

I would like to have as many gift per list that I want. I know that I need to change the type of the attribute gift_id in my database to do that. I don't know why symfony had generated an integer attribute and in which type I should change my attribute. I've been thinking of an array of id but I'm not sure.

Do you have any idea of how I could solve this please?

I already have a list_gift join table with two attributes list_id and gift_id. It's two attribute of type integer. Here are the files of my entities :

List.php

Declaration of the attribute used in the relationship

     /**
     * @ORMManyToMany(targetEntity=Gift::class, inversedBy="lists")
     */
    private $Gift;`

Function that allows you to add a gift to a list

    public function addGift(Gift $gift): self
    {
        if (!$this->Gift->contains($gift)) {
            $this->Gift[] = $gift;
        }

        return $this;
    }

Gift.php

Declaration of the attribute used in the relationship

     /**
     * @ORMManyToMany(targetEntity=List::class, mappedBy="Gift")
     */
    private $lists;

Could the problem come from my ListType form? Currently the field allowing to choose the gifts to put in my list is a dropdown list.

question from:https://stackoverflow.com/questions/65864491/manytomany-my-form-update-the-current-record-instead-of-adding-a-new-one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

...