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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:17:47+00:00 2026-06-14T05:17:47+00:00

Suppose to have an entity in Symfony2 that has a field bestfriend , which

  • 0

Suppose to have an entity in Symfony2 that has a field bestfriend, which is a User entity selected from a list of User entities that satisfy a complex requirement.
You can render this field in a form by specifying that it is an entity field type, i.e.:

$builder->add('bestfriend', 'entity', array(
   'class' => 'AcmeHelloBundle:User',
   'property' => 'username',
));

This form field is rendered as a <select>, where each one of the displayed values is in the form:

<option value="user_id">user_username</option>

So, one would render the field by using the <optgroup> tags to highlight such special feature of the friends.

Following this principle, I created a field type, namely FriendType, that creates the array of choices as in this answer, which is rendered as follows:

$builder->add('bestfriend', new FriendType(...));

The FriendType class creates a <select> organized with the same <option>s but organized under <optgroup>s.

Here I come to the problem! When submitting the form, the framework recognize that the user field is not an instance of User, but it is an integer. How can I let Symfony2 understand that the passed int is the id of an entity of type User?

  • 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-06-14T05:17:48+00:00Added an answer on June 14, 2026 at 5:17 am

    Here follows my solution.
    Notice that it is not mentioned in the Symfony2 official docs, but it works! I exploited the fact that the entity field type is child of choice.

    Hence, you can just pass the array of choices as a param.

    $builder->add('bestfriend', 'entity', array(
       'class' => 'AcmeHelloBundle:User',
       'choices' => $this->getArrayOfEntities()
    ));
    

    where the function getArrayOfEntities() is a function that fills the choice list with the friends of my friends, organized by my friends:

    private function getArrayOfEntities(){
        $repo = $this->em->getRepository('AcmeHelloBundle:User');
        $friends = $repo->findAllFriendByComplexCriteria(...);
        $list = array();
        foreach($friends as $friend){
            $name = $friend->getUsername();
            if(count($friend->getFriends())>0){
                $list[$name] = array();
                foreach($friend->getFriends() as $ff){
                    $list[$name][$ff->getUsername()] = $ff;
                }
            }
        }
        return $list;
    } 
    

    I know the example could be meaningless, but it works…

    PS: You need to pass the entity manager to let it working…

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

Sidebar

Related Questions

Suppose, we have an entity User , which has many comments . It is
suppose i have @Entity A, which has an eager @ManyToOne to @Entity B, and
Suppose I have the following data structure: a Customer entity that has a collection
Suppose I have 2 JPA entities: @Entity public class OwnerEntity { private List<OwnedEntity> subEntities
Suppose I have the following 2 entities: @Entity public class Person implements Serializable {
Suppose I have an entity in my datastore that I know it can have
Suppose I have a POJO User ,which I add some static method to do
Lets suppose I have a basic CustomerEntity which has the following attributes Name Surname
Suppose I have a blog post entity. It has many attributes It has comments
Suppose I have an entity class that stores a username: class Profile { private

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.