Skip to content
Advertisement

Symfony2 persist form collections with Doctrine UNIQUE INDEX and ORDER BY?

I have a User entity that has a collection of List Items associated with it.

Each List Item entity also references another entity, Topic. I have setup a UNIQUE constraint on the List Item table that will only allow unique combinations of the User and Topic foreign keys. No List Items with a duplicate reference to the Topic entity are allowed for each user. I am also ordering the results by “completion_week”.

There are times when I will be attempting to persist a form collection and it will fail with an integrity constraint violation. For some reason Symfony seems to think updates are being made to the form and is incorrectly attempting to update collection items – but is switching the a foreign key on some of the updated entities seemingly randomly – which is causing the error because of the above mentioned constraints.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '6-1' for key 'list_item_user_topic'

The User Entity:

<?php
/**
 * @ORMEntity(repositoryClass="AppMyBundleRepositoryUserRepository")
 * @ORMTable(name="users")
 * @GedmoSoftDeleteable(fieldName="deleted_at", timeAware=true)
 */
class User implements UserInterface, EquatableInterface
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=30, nullable=false)
     */
    private $first_name;

    /**
     * @ORMColumn(type="string", length=30, nullable=false)
     */
    private $last_name;

    /**
     * @ORMColumn(type="string", unique=true, length=100, nullable=true)
     */
    private $email;

    /**
     * @ORMOneToMany(
     *     targetEntity="AppMyBundleEntityListItem",
     *     mappedBy="user",
     *     orphanRemoval=true,
     *     fetch="EAGER",
     *     cascade={"all"}
     * )
     * @ORMOrderBy({"completion_week"="ASC"})
     * 
     */
    private $listItems;

    ...

The List Item Entity:

<?php
/**
 * @ORMEntity(repositoryClass="AppMyBundleRepositoryListItemRepository")
 * @ORMTable(
 *     name="list_items",
 *     uniqueConstraints={@ORMUniqueConstraint(name="list_item_user_topic", columns={"user_id","topic_id"})}
 * )
 *
 * @GedmoSoftDeleteable(fieldName="deleted_at", timeAware=true)
 */
class ListItem
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="AppMyBundleEntityUser", inversedBy="listItems", fetch="EAGER")
     * @ORMJoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    /**
     * @ORMColumn(type="integer", length=11, nullable=true)
     */
    private $completion_week;

    /**
     * @ORMManyToOne(targetEntity="AppMyBundleEntityTopic", inversedBy="listItems", fetch="EAGER")
     * @ORMJoinColumn(name="topic_id", referencedColumnName="id")
     */
    private $topic;

    ...

I am using Symfony2 form builder to build the form. This is working great. I have added javascript for add/remove buttons on the front end. In general – I am able to save and persist the form collection without any problems.

User Form Type:

<?php
/**
 * Class UserType
 * @package AppMyBundleFormType
 */
class UserType extends AbstractType
{
    /**
     * @param OptionsResolverInterface $resolver]
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'            => 'AppMyBundleEntityUser',
            'method'                => 'POST',
            'cascade_validation'    => true
        ));
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Set User data
        $user = $builder->getData();

        // Generate form
        $builder
            ->add('listItems', 'collection', array(
                'options'  => array(
                    'required' => false,
                    'attr' => array('class'=>'col-sm-12')
                ),
                'type'              => new ListItemType(),
                'label'             => false,
                'allow_add'         => true,
                'allow_delete'      => true,
                'delete_empty'      => true,
                'prototype'         => true,
                'by_reference'      => false
            ))
            ->add('first_name')
            ->add('last_name')
            ->add('email');
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'user';
    }
}

List Item Form Type:

<?php
/**
 * Class ListItemType
 * @package AppMyBundleFormType
 */
class ListItemType extends AbstractType
{
    /**
     * @param OptionsResolverInterface $resolver]
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'                => 'AppMyBundleEntityListItem',
            'method'                    => 'POST',
        ));
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Generate form
        $builder
            ->add('topic', 'entity', array(
                'attr'          => array('class' => 'form-control chosen-select-10'),
                'class'         => 'AppMyBundle:Topic',
                'empty_value'   => 'Choose a Topic',
                'label'         => false,
                'property'      => 'name',
                'expanded'      => false,
                'multiple'      => false
            ))
            ->add('completion_week', 'integer', array(
                'attr'          => array('class' => 'form-control'),
                'label'         => false,
            ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'list_item';
    }
}

What I discovered is that when the form is being processed – something is happening within the handleRequest() method that is swapping out foreign key references on different list items in the collection. In some cases – without making any changes to the form collection on the front end. Like so:

Original collection of List Items for a User:
Original collection of List Items for a User

User’s List Item Collection after handleRequest():
User's List Item Collection after handleRequest()

This then causes the integrity constraint violation when Doctrine attempts to write the first record because it is violating the unique constraint on the List Items table. What I do not understand is how/why the handleRequest() method would be swapping foreign keys on update.

Also – in many cases – the form will persist fine for a user. I hate to use the word “random” here but I have not been able to identify a way to duplicate the issue other than just working with the entity for a while and performing CRUD operations on it. Many times the form persists fine – other times the foreign key references get swapped and I am unable to submit the form to update the entity because of the UNIQUE constraint.

Has anyone experienced similar issues or have some insight on why this might be occurring? Is this a bug in the handleRequest() method? This will occur even if I have not made any changes to the List Item collection. As in – if I edit a user and simply submit the form without making any changes – this behavior will still occur.

Is there a better way to do this?

Advertisement

Answer

The solution was to add a Doctrine “IndexBy” annotation to the $listItems property on the User entity. By specifying a column here, the results returned will be indexed by its value. This must be a unique value. In this case I used the primary key.

/**
 * @ORMOneToMany(
 *     targetEntity="AppMyBundleEntityListItem",
 *     mappedBy="user",
 *     orphanRemoval=true,
 *     fetch="EAGER",
 *     indexBy="id",
 *     cascade={"all"}
 * )
 * @ORMOrderBy({"completion_week"="ASC"})
 * 
 */
private $listItems;

This then changed the way each of the collection items were indexed on the frontend.

From this:

<div class="row" data-content="user[listItems][0]">...</div>
<div class="row" data-content="user[listItems][1]">...</div>
<div class="row" data-content="user[listItems][2]">...</div>
<div class="row" data-content="user[listItems][3]">...</div>
<div class="row" data-content="user[listItems][4]">...</div>

To this:

<div class="row" data-content="user[listItems][1950]">...</div>
<div class="row" data-content="user[listItems][1951]">...</div>
<div class="row" data-content="user[listItems][1955]">...</div>
<div class="row" data-content="user[listItems][1953]">...</div>
<div class="row" data-content="user[listItems][1948]">...</div>

Now, when submitting the form, each collection item is referenced by it unique ID – ensuring that the data input on the frontend is persisted correctly after form binding.

The reason it was behaving somewhat randomly was because I was ordering the results by the “completion_week” column. There was a chance for the records to be returned in a different order where they share the same ORDER BY value. If you have three records with the same value for “completion_week” and you ORDER BY “completion_week” – it’s up to MySQL to determine the order of the results.

When Symfony received the POST results – the controller had to make another call to the database to get the User entity and build the form. If the results were returned in a different order, the array keys captured from the frontend would not match up – and the unique constraint error was produced onFlush.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement