Search code examples
symfonyeasyadmin

Easy-admin array field overwrites values


I am using version 2.7.0 of Doctrine and 2.3.4 of easycorp/easyadmin-bundle. My class person has a pet arrangement

/**
 * @ORM\Entity
 * /
class person
{
    // ...


    / **
     * @ORM \ Column (type = "array")
     *
     * /
    private $ pets = [];
}

The form to create or edit the person object and their pets works well, but sometimes in the edit view some values ​​are overwritten.


Solution

  • Solution:

    To solve this, all you have to do is put the pet's annotations as 'simple_array' instead of 'array'. It would look like this:

    /**
     * @ORM \ Entity
     * /
    class person
    {
        // ...
    
    
        / **
         * @ORM \ Column (type = "simple_array")
         *
         * /
        private $ pets = [];
    }
    

    Explanation:

    This happens because when you put a field like array then Doctrine in addition to saving the values ​​saves things like the index that had that value in the array, so if you have a person with the cat and dog values, it is stored as follows: a: 2: {i: 0; s: 3: "cat"; i: 1; s: 3: "dog";} which says that the arrangement has 2 elements, in the position 0 is cat and in position 1 is dog.

    Then each time the view is generated for editing, two inputs will be created with the names: person[pets][0] with the value cat and person[pets][1] with the value dog.

    However, if you click on the Add element button, the amount of input that the form already has will be taken into account so that it creates a new one with the name person[pets][2], if always you delete the last item in the list there will be no problems but if you remove for example the first input that has the name person[pets][0], the amount of input will be of value 1, so the new input that is create will be with the name person[pets][1] so you will overwrite the value of dog.

    However, when saving the field pets as simple_array in the database only the values ​​are saved: cat, dog, so every time the view is constructed to edit the indexes they will always start from scratch and will never be overwritten