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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:45:25+00:00 2026-06-18T05:45:25+00:00

I have a table as below: items -id (pk) AI -user_id (fk) references users

  • 0

I have a table as below:

items
-id (pk) AI
-user_id (fk) references users
-title
-description

I have a form page which requires authenticated users. I want to fill the select box with items that belongs to the user (like select * from items where user_id=loggedinUserId)

I looked at the documentation and I found this:

 $builder->add('items', 'entity', array(
    'class' => 'AcmeHelloBundle:Items',
    'query_builder' => function(EntityRepository $er) {
       return $er->createQueryBuilder('i')
        ->orderBy('i.title', 'ASC');
    },
));

My question is how can I pass the authenticated user id to this query in symfony 2.1 forms ?

  • 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-18T05:45:27+00:00Added an answer on June 18, 2026 at 5:45 am

    You have two possibilities:

    • inject the parameters via the constructor (Constructor Injection)
    • inject the parameters via the options of the form

    This awesome thread show you an interesting way of doing it (from @khepin). However, as suggested by @Bernhard (see first comment), there is a simpler way in that case.


    METHOD 1- Constructor Injection : If you can’t bother creating a suscriber etc… you can directly inject the security context into your form constructor :

    ItemType:

    namespace Acme\HelloBundle\Form\Type;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use Symfony\Component\Security\Core\SecurityContextInterface;
    use Symfony\Component\Security\Core\User\UserInterface;
    
    classItemType extends AbstractType
    {
        /**
         * @var string
         */
        private $class;
    
        /**
         * @var UserInterface
         */
        private $user;
    
        /**
         * @param string $class
         */
        public function __construct($class, SecurityContextInterface $securityContext)
        {
            $this->class = $class;
            $this->user = $securityContext->getRequest->getUser();
        }
    
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
    
                    $username = $this->user->getUsername();    
    
                    $builder->add('items', 'entity', array(
                        'class' => $this->class,
                        'multiple' => true,
                        'expanded' => true,
                        'query_builder' => function(EntityRepository $er) use ($username) {
                            $query = $er->createQueryBuilder('i')
                                ->select(array('i'))
                                ->leftJoin('i.users', 'u')
                                ->andWhere('u.username = :username')
                                ->setParameter('username', $usename)
                                ->orderBy('i.title', 'ASC');
    
                            return $query;
    
                        },
                    )
                ));
    
        }
    
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => $this->class,
            ));
        }
    
        public function getName()
        {
            return 'acme_hello_item';
        }
    }
    

    Declare it as a service:

    <parameters>
        <parameter key="acme_hello.item.class">Acme\HelloBundle\Entity\Item</parameter>
    </parameters>
    
    <services>
        <service id="merk_notification.filter.form.type" class="Acme\HelloBundle\Form\Type\ItemType">
            <tag name="form.type" alias="acme_hello_item" />
    
            <argument>%acme_hello.item.class%</argument>
            <argument type="service" id="security.context" />
        </service>
    </services>
    

    To build your form, you can now do :

        $formBuilder = $this->container->get('form.factory');
        $form = $formBuilder->createNamed('acme_hello_item', 'acme_hello_item');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below table in a .JSP page: <form:form method=post action=update.dtt id=contactForms modelAttribute=contactForms
In my app, I have a page where I want admin users to be
I have a table like below: COL1 | COL2 | COL3 ______________________ ITEM1 |
i have the table below <tr id=group_1>...</tr> <tr id=el>...</tr> <tr id=el>...<tr> <tr id=group_2></tr> <tr
I have a table like below: Table A ---------- ID Field1 Field2 Field3 1
I have a table as below ID Date 1 Null 1 Null 1 Null
I have a table as below Name Priority Date ------------------------- A 2 d1 B
I have a table like below: EmployeeId EmployeeName RequestId RequestName EmployeeId RequestId I need
I have a table as below dbo.UserLogs ------------------------------------- Id | UserId |Date | Name|
I have a table with below structure. I am not having control over changing

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.