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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:47:27+00:00 2026-06-08T17:47:27+00:00

I want to create a simple blog example where users have a favourite category

  • 0

I want to create a simple blog example where users have a favourite category attached to there account. This means the can only write articles for this category. (Some of them – mostly admins -will get the opportunity to switch categories, but that’s not the problem… for now ^^)

So I first created a relation between the User- and the Category entity. Everything works fine. Each user now has a main category selected.

Only thing which bothers me, is that I cant get hold of the current logged in user in the EntityType (formbuilder) and EntityRepository classes.

In my “New Post” form there are relations to other entities (e.g. Tags). I use the ‘entity’ formtype in the EntityType class to generate these form elements. Now i wan’t to filter the tags, to only allow tags which have the same category relation as the currently logged in users category to be selectable.

I tried to use the query_builder option from the entity formtype. But as i can’t get the current user object, I don’t know which category he has selected. Same problem with the EntityRepository.

Now I could filter the tags already in the PostController but the problem is, that I will need this over and over again. And therefore I don’t wan’t to code this everytime I add something new.

I thought it would be the best to place this filter in the EntityRepository. So I can always access the findAllByCategory. But I need the user-object in there.

What is the best way to accomplish this? Have searched a lot, but either I searched for the wrong terms or no one has this problem 🙂

  • 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-08T17:47:28+00:00Added an answer on June 8, 2026 at 5:47 pm

    You can inject security context in your form type defined as a service. Then in your tags field use the query builder with $user (current logged user) to filter tags which have the same category relation as the currently logged:

    /** @DI\Service("form.type.post") */
    class PostType extends \Symfony\Component\Form\AbstractType
    {
        /**
         * @var \Symfony\Component\Security\Core\SecurityContext
         */
        protected $securityContext;
    
        /**
         * @DI\InjectParams({"securityContext" = @DI\Inject("security.context")})
         *
         * @param \Symfony\Component\Security\Core\SecurityContext $context
         */
        public function __construct(SecurityContext $securityContext)
        {
            $this->securityContext = $securityContext;
        }
    
        public function buildForm(FormBuilder $builder, array $options)
        {
            $user = $this->securityContext()->getToken()->getUser();
    
            $builder->add('tags', 'entity', array(
                'label'         => 'Tags',
                'class'         => 'Acme\HelloBundle\Entity\Tag',
                'property'      => 'name',
                'query_builder' => function(EntityRepository $er) use($user) {
                    return $er->getAllSuitableForUserChoiceQueryBuilder($user);
                },
                'multiple'      => true,
                'expanded'      => true,
        }
    }
    

    Filter tags into your repository:

    class TagRepository extends EntityRepository
    {
        public function getAllSuitableForUserChoiceQueryBuilder(User $user)
        {
            // Filter tags that can be selected by a given user
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I want to create a simple blog where users can post comments, but
I have created a simple project where everyone can create one or more Blog.
I want to create a simple app, where the user can take a photo.
I want to, create blog page in my website. There are bunch of huge
I create a simple blog with cakephp where i have some posts and some
I want to create a timeline WPF control (as in this (http://www.taterboy.com/blog/images/MotionEditor/classic_vs_custom.png) kind of
I am using CakePHP to create a simple blog for myself. I want to
starting from the rails blog tutorial, i want to have listing and create functionality
I want to create a blog-like website. At the begining I'll need a simple
I want to create simple app able to edit images. Main view of app

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.