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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:21:34+00:00 2026-06-14T17:21:34+00:00

I’m wondering what is the best way to create a form that handle a

  • 0

I’m wondering what is the best way to create a form that handle a many-to-many relation with extra field in symfony2.

For the example let’s say I would like to create a notifying feature for users. I have the User and the Notification entities. Also in order to be able to add extra parameters in the many-to-many relation between entities, I created a third entity UserNotification. The extra parameter indicates if the notification has been read or not by the users.

* @ORM\Entity() */
class Notification
{
    /** @Id @Column(type="integer") */
    private $id;

    /** @OneToMany(targetEntity="UserNotification", mappedBy="notification") */
    private $usernotifications;

    /** @Column() */
    private $message;

    ...
}

* @ORM\Entity() */
class User
{
    /** @Id @Column(type="integer") */
    private $id;

    /** @OneToMany(targetEntity="UserNotification", mappedBy="user") */
    private $usernotifications;

    /** @Column() */
    private $name;

    ...
}

* @ORM\Entity() */
class UserNotification
{
    /** @ManyToOne(targetEntity="User", inversedBy="usernotifications")
    private $user;

    /** @ManyToOne(targetEntity="Message", inversedBy="usernotifications")
    private $message;

    /** @Column(type="boolean") */
    private $isRead;

    ...
}

This way I am able to get this kind of data

      User
+----+---------+
| id | name    |
+----+---------+
|  1 | John    |
|  2 | Paul    |
+----+---------+

      Notification
+----+----------------------+
| id | message              |
+----+----------------------+
|  1 | Cool stuff           |
|  2 | Lorem ipsum          |
+----+----------------------+

      UserNotification
+---------+-----------------+---------+
| user_id | notification_id | isRead  |
+---------+-----------------+---------+
|  1      |              1  |       1 |
|  2      |              1  |       0 |
|  1      |              2  |       0 |
|  2      |              2  |       1 | 
+---------+-----------------+---------+

Ok now here is the problem, how to make a form which allow to send a notification to some users? With a classic many-to-many relation it was not a problem. I had a NotificationType with the following builder:

    $builder->add('users', 'entity', array(
                'class' => 'User',
                'property' => 'name',
                'multiple' => 'true',
            ))
            ->add('message', 'text');

But since I had to create the intermediate UserNotification entity, I have no idea how to do it. Thanks for your help 😉

  • 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-14T17:21:35+00:00Added an answer on June 14, 2026 at 5:21 pm

    I did exactly the same. My InternalMessage entity is your Notification. In the form, users field is not mapped (property_path is false) and it’s validated using a subscriber:

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('title', 'text', array(
                'label' => 'Titolo *'
            ))
            ->add('content',  'textarea', array(
                'label' => 'Contenuto *'
            ))
            ->add('priority', new PriorityType(), array(
                'label' => 'Priorità *'
            ))
            ->add('users', 'entity', array(
                'label'         => 'Destinatari *',
                'class'         => 'Acme\HelloBundle\Entity\User',
                'property'      => 'select_label',
                'multiple'      => true,
                'expanded'      => true,
                'property_path' => false,
            ));
        ;
    
        $builder->addEventSubscriber(new AddUsersValidationSubscriber());
    }
    

    While in my controller, the only thing i have to do (assuming that form is valid) is to create one InternalMessageFeedback entity (the same as your UserNotification) fore each user in form model:

    $message = new InternalMessage();
    $form    = $this->createForm(new InternalMessageType(), $message);
    
    // ...
    
    // The sender
    $message->setUser($this->getSecurityContext()->getToken()->getUser());
    
    // Persist the inverse side
    $em = $this->getEntityManager();
    $em->persist($message);
    
    // One feedback for each user
    /** @var $user \Acme\HelloBundle\Entity\User */
    foreach($form->get('users')->getData() as $user) {
        $feedback = new InternalMessageFeedback();
    
        // Se the message and user for current feedback
        $feedback->setInternalMessage($message);
        $feedback->setUser($user);
    
        // Persist the owning side
        $em->persist($feedback);
    
        // Sync the inverse side
        $message->addInternalMessageFeedback($feedback);
    }
    
    $em->flush();
    

    Hope this helps 🙂

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

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.