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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:59:24+00:00 2026-06-09T23:59:24+00:00

I have created a database and a bundle that manages users, guests, categories: A

  • 0

I have created a database and a bundle that manages users, guests, categories:

  • A user can have many guests
  • A guest can belong to many categories
  • Many guests (of a user) can belong to a category
  • A user can have many categories

I have set the relationships as below:

User -> OneToMany -> Guests  
Guests <-> ManyToMany <-> Categories  
User -> OneToMany -> Categories  

Guest entity

class Guest
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
    * @ORM\ManyToOne(targetEntity="Acme\SomethingBundle\Entity\User", inversedBy="guests")
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
    */
    protected $user;

    /**
    * @ORM\ManyToMany(targetEntity="Category", inversedBy="guests")
    * @ORM\JoinTable(name="guests_categories")
    */
    protected $categories;

User entity

class User implements UserInterface
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
    * @ORM\OneToMany(targetEntity="Acme\SomethingBundle\Entity\Guest", mappedBy="user")
    */
    protected $guests;

    /**
    * @ORM\OneToMany(targetEntity="Acme\SomethingBundle\Entity\Category", mappedBy="user")
    */
    protected $categories;

Category entity

class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
    * @ORM\ManyToOne(targetEntity="Acme\SomethingBundle\Entity\User", inversedBy="categories")
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
    */
    protected $user;

    /**
    * @ORM\ManyToMany(targetEntity="Guest", mappedBy="categories")
    */
    protected $guests;

The user must be able to view ONLY his guests and categories – they are not shared or something. I have successfully retrieved a list of a user’s guests and their categories with a DQL query.

public function indexAction()
    {

        $user = $this->get('security.context')->getToken()->getUser();
        $userId = $user->getId();

        $em = $this->getDoctrine()->getEntityManager();
        $query = $em->createQuery( "SELECT g, u, c
                                    FROM Acme\SomethingBundle\Entity\Guest g
                                    JOIN g.user u
                                    LEFT JOIN g.categories c
                                    WHERE u.id = :userId
                                    ORDER BY g.surname ASC");
        $query->setParameter('userId', $userId); 
        $entities = $query->getResult();

        return $this->render('AcmeSomethingBundle:Guest:index.html.twig', array(
            'entities' => $entities
        ));
    }

and a twig interface:

{% block body %}
    {% for entity in entities %}
    <div class="guestBox">
            <div class="guestName" >{{ entity.surname }} {{ entity.name }}</div>
            {% for category in entity.categories %}
            {{ category.name }}
            {% endfor %}
        </div>
    </div>
    {% endfor %}
{% endblock %}

Where is the problem ? When I am trying to edit a guest entry (a form in twig), all the categories are listed in a multiple-select box, even those that haven’t created by the current logged in user. I can’t manage to get the user’s specific categories :

public function editAction($id)
    {
        $user = $this->get('security.context')->getToken()->getUser();
        $userId = $user->getId();

        $em = $this->getDoctrine()->getEntityManager();
        $query = $em->createQuery( "SELECT g, c, u
                                    FROM AcmeSomethingBundle:Guest g
                                    JOIN g.user u
                                    LEFT JOIN g.categories c 
                                    WHERE u.id = :userId
                                    AND g.id = :guestId");
        $query->setParameter('guestId', $id)
              ->setParameter('userId', $userId);
        $entity = $query->getSingleResult();

        if (!$entity) {
            throw $this->createNotFoundException('Wrong guest id');
        }

        $editForm = $this->createForm(new GuestType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('AcmeSomethingBundle:Guest:edit.html.twig', array(
            'entity'     => $entity,
            'editForm'   => $editForm->createView(),
            'deleteForm' => $deleteForm->createView()        
            ));
    }

and a twig template:

{% block body %}

<form action="{{ path('guest_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(editForm) }}>

        {{ 'guest.form.categories'|trans }}
        {{ form_errors(editForm.categories) }}
        {{ form_widget(editForm.categories) }}

        <button type="submit" class="btn guestSave">{{ 'guest.form.save'|trans }}</button>
</form>

{% endblock %}

Some more information: Symfony profiler shows many queries:

SELECT t0.id AS id1, t0.username AS username2, t0.salt AS salt3, t0.password AS password4, t0.email AS email5, t0.is_active AS is_active6 FROM users t0 WHERE t0.id = ?
Parameters: ['2']

SELECT g0_.id AS id0, g0_.name AS name1, g0_.surname AS surname2, g0_.email AS email3, g0_.address AS address4, g0_.phone AS phone5, g0_.mobile AS mobile6, g0_.description AS description7, g0_.created_at AS created_at8, g0_.updated_at AS updated_at9, g0_.is_activated AS is_activated10, u1_.id AS id11, u1_.username AS username12, u1_.salt AS salt13, u1_.password AS password14, u1_.email AS email15, u1_.is_active AS is_active16, c2_.id AS id17, c2_.name AS name18, c2_.description AS description19, g0_.user_id AS user_id20, c2_.user_id AS user_id21 FROM guest g0_ INNER JOIN users u1_ ON g0_.user_id = u1_.id LEFT JOIN guests_categories g3_ ON g0_.id = g3_.guest_id LEFT JOIN category c2_ ON c2_.id = g3_.category_id WHERE u1_.id = ? AND g0_.id = ?
Parameters: ['2', '33']

SELECT t0.id AS id1, t0.name AS name2, t0.description AS description3, t0.user_id AS user_id4 FROM category t0
Parameters: { }

Any help will be greatly appreciated.

  • 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-09T23:59:25+00:00Added an answer on June 9, 2026 at 11:59 pm

    Probably you should define in more specific way which categories will be passed to your GuestType form.

    You can do that by passing option ‘query_builder’ to your form’s builder. Eg. in GuestType will be something like that:

    $builder->add(...)
            ->add('category', 'entity', array (
                ...
                'label' => 'Some label',
                'required' => false,
                'query_builder' => function(CategoryRepository $repository) use($user) {
                     // should return doctrine's QueryBuilder object to create query which returns categories for specified $user                      
                }))
            ->add(...);
    

    Look here: http://symfony.com/doc/current/reference/forms/types/entity.html#query-builder

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

Sidebar

Related Questions

I have a database that I have created using SQL Server Developer 2008. I
I have created a dynamic array of buttons that come from sqllite database. For
I have created my database by this sql query: (CREATE DATABASE + DBName +
I have created a database of my movies and another with my actors in
I have created my database, and used cake bake my_project to create my project,
I have created SQL Server database by Visual Studio 2010 Ultimate means in Visual
I have created a MySQL database using MySQL Workbench. It has about 20 tables.
I have created the following database layout, and started coding the application. The more
I have created SQL Server 2008 Database project using visual studio 2010. after build
I have a database created by: -- Database: adatabase -- DROP DATABASE adatabase; CREATE

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.