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 4607144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:39:42+00:00 2026-05-22T00:39:42+00:00

In symfony 2.0, how to create a drop down list using one-to-one association in

  • 0

In symfony 2.0, how to create a drop down list using one-to-one association in form? Can you guys put good example please?

  • 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-22T00:39:43+00:00Added an answer on May 22, 2026 at 12:39 am

    I will try to answer your question the way I understand it. Let’s say I have a Faculty object bound to a single University object. So in the form used to create or edit a faculty, I display a combo box of all the university in the database and the user choose one among them. There is one special Symfony field type that does exactly this: the entity type. Below is the code of the buildForm method that I use in my FacultyType object used to create the faculty form:

    // Application\AcmeBundle\Form\Type\FacultyType
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('name');
        $builder->add('university', 'entity', array(
            // The class of the entity used as a combo box item
            'class' => 'AcmeBundle:University',
    
            // The property of the entity displaying the entity as text
            'property' => 'name',
    
            // The query builder used to populate the combo box, accepts
            // a QueryBuilder object or a \Closure like below 
            'query_builder' => function(EntityRepository $repository) {
                // This will return a query builder selecting all universities
                return $repository->createQueryBuilder('u');
            }
        ));
    }
    

    Note: There are other properties that can be set for the entity field type, I invite you to take a look at this page for more information on it.

    Rendered, this will show a combo box with all the universities I have set in the database. When the user save the form, the university chose is assigned to the faculty object bound to the form via a setter. You could probably render a drop-down list instead of a combo box. If you need to select multiple entities, the 'multiple' option of the field type entity could be useful.

    This being said, the example I showed is not a One-to-One relation but rather a Many-to-One for the Faculty object and a One-to-Many for the University object. A One-to-One relation would be something more like a relation where a University has a unique Address. In this case, a combo box wouldn’t be useful since the university can only have one adress so a sub-form would be more appropriate. If it has many adresses, then it becomes a One-to-Many relation like the relation between the university and its faculties.

    Not sure if this will answer your question correctly but I hope it will lead you to a final solution.

    Regards,
    Matt

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

Sidebar

Related Questions

I'm looking for something like break for loops. Here's some example code (using Symfony's
I'm using Lightwindow v2 in a Symfony project to create a modal window (
I'm using Symfony 1.4 and I have to create pdf file from Word/HTML with
i am using Symfony 1.4, to create my project. i have tried to deploy
I am using symfony 1.4, to create my project with propel as ORM. i
I use symfony 1.4.10 I have next situation: User can create Ads and he
I am using sfOpenID plugin for Symfony, which doesn't support OpenID 2.0. That means,
I'm using Symfony 1.4 and Doctrine. Let's say I have 2 classes : a
I am working in PHP(with Symfony Framework) and i want to create a search
In Symfony 1.2, how do you create variables in actions.class.php so that they are

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.