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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:37:07+00:00 2026-05-29T19:37:07+00:00

Let’s say I have ordinary *Type class: class LocationType extends AbstractType { /** *

  • 0

Let’s say I have ordinary *Type class:

class LocationType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add(...)
            ...
    }
}

and one of the fields is a choice type. The values that need to be used as choice items are supposed to be retrieved from the database (from some particular entity repository).

So the question is: how to get the repository in the LocationType class? Is passing it through the constructor the only way to get it?

UPD:

I know about entity type but unfortunately I cannot use it, because my property is not and cannot be defined as one-to-one relation due to very complex relation conditions that Doctrine doesn’t support (yet?). See How to specify several join conditions for 1:1 relationship in Doctrine 2 for additional details

  • 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-29T19:37:12+00:00Added an answer on May 29, 2026 at 7:37 pm

    You can specify an entity field type as an option like so:

    $builder
        ->add('foo', 'entity', array(
            'class'  => 'FooBarBundle:Foo',
            'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
                 return $er->createQueryBuilder('q')->orderBy('q.name', 'ASC');
             },
         ));
    

    EDIT:
    Actually the ‘class’ option is the only required field option. You can read a bit more about the entity field type here: http://symfony.com/doc/2.0/reference/forms/types/entity.html

    Hope this helps.

    EDIT:

    Further to discussion below, here’s an example

    In the controller:

    $entity = new Foo();
    $type   = new FooType();
    
    $er = $this->getDoctrine()
        ->getEntityManager()
        ->getRepository('FooBarBundle:Foo');
    
    $form = $this->createForm($type, $entity, array(
        'foo_repository' => $er
    ));
    

    The $options array is passed to the FooType::buildForm() method, so foo_repository should then be available in this method like so:

    $er = $options['foo_repository'];
    

    Symfony 4 and 5:

    Symfony Form Types are services so you can use dependency injection:

    class FooType extends AbstractType
    {
        private $entityManager;
    
        public function __construct(EntityManagerInterface $entityManager)
        {
            $this->entityManager = $entityManager;
        }
    
        private function getFooRepository(): FooRepository
        {
            return $this->entityManager->getRepository(Foo::class);
        }
    
        ...
    }
    

    or inject specific repository:

    class FooType extends AbstractType
    {
        private $fooRepository;
    
        public function __construct(FooRepository $fooRepository)
        {
            $this->fooRepository = $fooRepository;
        }
    
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following entity: public class Store { public List<Product> Products
Let's imagine I have a Java class of the type: public class MyClass {
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say for example, I have the following javascript function that returns a boolean:
Let's say I'm building a data access layer for an application. Typically I have

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.