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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:12:15+00:00 2026-05-26T09:12:15+00:00

I am using Symfony 2 with doctrine. I currently have an entity called Worker

  • 0

I am using Symfony 2 with doctrine. I currently have an entity called Worker and in the Worker entity there is a Many To One relationship with a User entity.

/**
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

There are more entities like Worker as well such as Manager and such. I want to create a form that creates a Job entity. In the form I am trying to create a select option that selects a Worker, but the worker’s name is stored in the user database. Is there any way to print the worker’s name from the user database in the form options.

$builder->add('workers','entity', [
    'label' => 'Workers:',
    'property' => 't.user.firstName',
    'empty_value' => 'Choose a Worker',
    'class' => 'Company\CompanyBundle\Entity\Worker',
    'query_builder' => function (\Company\CompanyBundle\Repository\WorkerRepository $repository) {
            return $repository->createQueryBuilder('t')
                ->add('orderBy', 't.user.firstName ASC');
        }
    ]);

Any ideas?

  • 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-26T09:12:16+00:00Added an answer on May 26, 2026 at 9:12 am

    I think that it would be enoough to do something like this:

    $builder->add('workers', 'entity', array(
                'class' => 'Company\CompanyBundle\Entity\Worker',
            ) );
    

    Besides, you should implement a “__toString()” method in your Worker entity where you would return whatever you want to show (in this case, the worker name), so your __toString method in the Worker entity would be something like this:

    function __toString() {
        return $this->getName();
    }
    

    It’s the way I usually implement this kind of relations, I hope it helps!

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

Sidebar

Related Questions

I'm using Symfony and Doctrine, and have several many-to-many relations that work fine. But
I'm currently designing a website, using symfony (1.2) with Doctrine as an ORM. I
I'm using Symfony 1.4 with Doctrine. I have a doctrine class called Image and
Using Symfony and Doctrine, I have a multi-select list box. The multiple default values
Right now I'm using Doctrine2 in conjunction with Symfony2. I have an entity called
I am using Symfony 2 with Doctrine 2. I have a UserListener ( symfony
I have a project created using Symfony 1.4.15. Because I like Doctrine :) I
I have been looking at nestedSet behaviour in symfony using doctrine, to display a
Is there any way to run this sql query in Doctrine using symfony ??
I have a little disturbing problem in here! an using symfony 1.4 with Doctrine!

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.