Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to handle large Form Collection in Symfony2

Tags:

forms

symfony

Edited my post to be similar to Symfony Cookbook and added some code.

http://symfony.com/doc/current/cookbook/form/form_collections.html

Note that the Entity/Form code posted in the part is the same as the one in the doc linked above.

I have a "Task" entity, which is linked to a "Tag" entity.To keep it simple, "Task" has a single field "description", and a "tag" has a single field "name".A "Tag" is linked to one "Task", and a "Task" is linked to many "Tags".

Entity:

class Task
{
    protected $description;
    protected $tags;

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

    public function getDescription()
    { return $this->description;}

    public function setDescription($description)
    { $this->description = $description; }

    public function getTags()
    { return $this->tags; }

    public function setTags(ArrayCollection $tags)
    { $this->tags = $tags; }
}

class Tag
{
    public $name;
}

At the moment, I use a Collection of "Tags" in the "Task" form to edit all them at once, as described in Symfony CookBook:

Form:

class TagType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TaskBundle\Entity\Tag',
        ));
    }

    public function getName()
    {
        return 'tag';
    }
}


class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('description');

        $builder->add('tags', 'collection', array('type' => new TagType()));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TaskBundle\Entity\Task',
        ));
    }

    public function getName()
    {
        return 'task';
    }
}

But a problem appears when I create a collection of "Tags" larger than a thousand elements. At this time, the form take seconds and seconds to load, and sometimes it crashes due to memory.

$task = new Task();

$tag1 = new Tag();
$tag1->name = 'tag1';
$task->getTags()->add($tag1);
$tag2 = new Tag();
$tag2->name = 'tag2';
$task->getTags()->add($tag2);
//Create a couple thousand more item here ...

//The script crashes here, when the form is being created
$form = $this->createForm(new TaskType(), $task);

The error does not come from Doctrine, which handles very well the whole thing, but from Symfony Form.

Is it a good idea to handle my form using Symfony2 built-in form system (with Collections) in this case, or should I handle it like in the old days, with raw html inputs and server-side validation/saves?

like image 694
elwood Avatar asked Nov 12 '22 15:11

elwood


1 Answers

I'm wondering if your issue has nothing to do with the form part of this, but the hydration of the tag objects. If you're asking doctrine to hydrate a ton of objects, it's going to use a big chunk of memory. You may want to look into another method of hydrating the tags, perhaps HYDRATE_ARRAY instead of HYDRATE_OBJECT.

like image 114
Mike Avatar answered Nov 15 '22 09:11

Mike