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

  • SEARCH
  • Home
  • 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 7567691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:43:41+00:00 2026-05-30T14:43:41+00:00

I have an entity type form field in my Symfony2 project. $builder = $this->createFormBuilder();

  • 0

I have an entity type form field in my Symfony2 project.

$builder = $this->createFormBuilder();
$projects = $this->getProjects();

$builder->add('project', 'entity',
        array(
            'class' => 'MyBundle:Project',
            'required' => false,
            'choices' => $projects,
        ));

The problem I’m having is, when the getProjects() method will return an empty result set, the drop down list will have all the projects in the Project table.

Is there any way to disable this behavior?

  • 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-30T14:43:42+00:00Added an answer on May 30, 2026 at 2:43 pm

    I believe the unexpected behaviour is occurring because you are mixing up the use of the Choice and Entity form field types.

    You are specifying an Entity field (second parameter to $builder->add()) and then attempting to populate it with values using the ‘choices’ option. However, the ‘choices’ option is not directly applicable to the Entity field type, although it is said to inherit from Choice. Rather, an Entity field is intended to automatically load the choices from the database for you. If you set only the Entity ‘class’, the field is populated with all the entities from the table in ascending primary key order. In order to load a subset of entities and/or load them in a particular order you can set a ‘query_builder’ function.

    For example, to create a drop-down list of all countries in ascending name order:

    $builder->add('country', 
                  'entity', 
                  array('class' => 'My\Bundle\Entity\Country',
                        'property' => 'name', 
                        'query_builder' => function(EntityRepository $er) {
                            return $er->createQueryBuilder('country')
                                      ->orderBy('country.name', 'ASC');
                        },
                        'required' => true, 
                        'empty_value' => false));
    

    The query can be as simple or complex as required. See Using Doctrine’s Query Builder.

    I suspect that the way the project field is created in the question causes the underlying choices for the drop-down to be set twice – firstly when the ‘class’ option is set, to all the available project entities, secondly when the ‘choices’ option is set, to the result of $this->getProjects(). Presumably if the latter is an empty array it does not override the former and thus all the projects appear in the list.

    If for some reason you can’t use a query builder to get the projects for the drop-down then you can use a Choice field type and map the projects data into the ‘choices’ option manually. For example, something like this:

    $builder = $this->createFormBuilder();
    $projects = $this->getProjects();
    
    $projectChoices = array();
    foreach ($projects as $project) {
        $key = $project->getId();
        $value = $project->getName();
        $projectChoices[$key] = $value;
    }
    
    $builder->add('project', 
                  'choice',
                   array('choices' => $projectChoices,
                         'required' => false));
    

    Note that in this case the value of the ‘project’ field will be a Project id whereas for an Entity field it will be an actual Project entity, which is another reason why it is preferable to use an Entity field.

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

Sidebar

Related Questions

I have a field aliases of type java.util.Set in one Entity . This Set
In my form I have field of type entity . How to disable validation
I have form with checkboxes loaded from database (I use entity field type). Checkboxes
I have some form, and one column have Entity type, but this entity have
I have a property of type uint on my entity. Something like: public class
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
I have a lookup field in the form, before select the related entity for
I have this entity service in my domain model with two datetime type properties
I have two entities types: RunContainer parent entity type Run child entity type Run
I have a table with tax rates where NULL entity type rows represent the

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.