Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:38:20+00:00 2026-05-27T14:38:20+00:00

I’ve spent a while looking through the symfony2 docs trying to find a suitable

  • 0

I’ve spent a while looking through the symfony2 docs trying to find a suitable method of doing what I need to do, maybe I’m looking in the wrong place.

Basically, I have an entity called Album, which can have many Subalbums associated with it. As the user is creating an Album entity using a form, I want them to be able to create quick Subalbum entities in-line, which will get saved later on. I also want to display the field in a custom format, so I don’t want to use a select tag with a multiple attribute, instead I’m manually rendering it in Twig (I haven’t had a problem with this).

The relationship on Subalbum is defined as follows:

/**
 * @ORM\ManyToOne(targetEntity="Vhoto\AlbumBundle\Entity\Album")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
 */
protected $parent;

Here’s what I’ve tried so far…

  1. Use an entity type field in the form builder, and then manually output the field. The issue I have with using the entity field is that if the user creates a Subalbum inline, symfony2 doesn’t like it when I submit the form because it has no ID.

  2. Use a hidden field type and try submitting multiple entries under the same field name (album[subalbums][]). Symfony2 also doesn’t like this when I submit the form

I guess I’m going to have to have a prePersist method in my Album entity to create any Subalbum entities that the user has created inline?

Hopefully there is a far more graceful solution that I’m just completely overlooking.

Let me know if anything is unclear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:38:21+00:00Added an answer on May 27, 2026 at 2:38 pm

    There is indeed a better way to do it.

    Entity creation

    Create the two Entity POPOs and assign a many-to-one relationship to one of the fields of the child entity (you have done this correctly). You might also want to define a one-to-many relationship in the parent

    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Child", mappedBy="parent", cascade={"persist", "remove" }, orphanRemoval=true)
     */
    protected $children;
    

    I am not sure if it’s necessary, but you should explicitly set the relationship in your setters just to be sure. For example in your owning entity:

    public function addChild(ChildInterface $child)
    {
        if(!$this->hasChild($child))
        {
            $this->children->add($child);
            $child->setParent($this);
        }
    }
    

    Doctrine doesn’t probably use these methods to bind the post data, but having this for yourself might take care of several persisting issues.

    Form type creation

    Create a form type for both entities

    /**
     * This would be the form type for your sub-albums.
     */
    class ChildType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            //$builder->add(...);
            //...
        }
    
        public function getDefaultOptions(array $options) {
            return array(
                'data_class' => 'Acme\Bundle\DemoBundle\Entity\Child'
            );
        }
    
        public function getName()
        {
            return 'ChildType';
        }
    }
    
    /**
     * This would be the form type for your albums.
     */
    class ParentType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
            // This part here describes the relationship between the two
            // form types.
            $builder->add('children', 'collection', array(
                'type' => new ChildType(),
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true
            ));
        }
    
        public function getName()
        {
            return 'ChildType';
        }
    }
    

    With the options allow_add and allow_delete you’ve effectively told Symfony, that a user can add or remove entities from the collection. The prototype option lets you have a prototype of the so-called sub-form on your page.

    Controller

    You should enforce the relationship here as well just to be safe. I have tucked this away in a separate entity manager layer (I have separate managers for more complicated entities), but you can most certainly do this in your controllers as well.

    foreach($parent->getChildren() as $child)
    {
        if($child->getParent() === NULL)
        {
            $child->setParent($parent);
        }
    }
    

    View

    Prepare your form’s template. The prototype should be rendered by calling form_rest(form) somewhere in your template. In case it doesn’t or you’d like to customize the prototype, here’s a sample of how to do it.

    <script id="ParentType_children_prototype" type="text/html">
        <li class="custom_prototype_sample">
            <div class="content grid_11 alpha">
                {{ form_widget(form.children.get('prototype').field1) }}
                {{ form_widget(form.children.get('prototype').field2) }}
                {{ form_rest(form.children.get('prototype') ) }}
            </div>
        </li>
    </script>
    

    You’d have to make the form dynamic by using JavaScript. If you use jQuery, you can access the prototype by calling $('ParentType_children_prototype').html(). It is important to replace all occurrences of $$name$$ in the prototype with the proper index number when adding a new child to the parent.

    I hope this helps.

    EDIT I just noticed there’s an article in the Symfony2 Form Type reference about CollectionType. It has a nice alternative on how to implement the front-end for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.