2

I'm new with symfony, I looked around but I didn't find the right answer to my problem.

I have two entities linked with a many-to-many relation. Entity User -> Entity FollowedUser. One User should be able to follow several FollowedUser and one FollowedUser should has several Users who follow him.

My problem is that when I try to list all FollowedUser for one User, say my CurrentUser, I get all FollowedUser not only those associated to my CurrentUser.

Here is my code.

User Entity (src/MyBundle/Entity/User.php) :

namespace MyBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="My_user")
 */
class User extends BaseUser

    // ...

    /**
     * @var FollowedUser[] $followedUsers 
     * 
     * @ORM\ManyToMany(targetEntity="MyBundle\Entity\FollowedUser")
     */
     private $followedUsers;

     // ...

     public function getFollowedUsers()
     {
         return $this->followedUsers;
     }
}

UserType:

namespace MyBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use MyBundle\Entity\FollowedUserRepository;

class UserType extends AbstractType
{

    public function buildForm(FormBuilder $builder, array $options)
    {        
        $builder->add('followedUsers'); // This shows me the whole table
        //,'entity' , array('class' => 'MyBundle\Entity\FollowedUser',
        //                'multiple' => true,
        //                  'query_builder' => function(FollowedUserRepository $followedUserRepo) use ($options) {
        //                                         $followedUsers = $options['data']->getFollowedUsers();
        //                                         $choices = array();
        //                                         foreach ( $followedUsers as $followedUser){
        //                                             $choices[] = $followedUser->getId();
        //                                         }
        //                                         $qb = $followedUserRepo->createQueryBuilder('u');
        //                                         $qb->select('u')
        //                                            ->where( $qb->expr()->in('u.id',$choices));
        //                                         return $qb;
        //                                        }          
        //         ));
     }

    public function getName()
    {        
        return 'followedUser';
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'MyBundle\Entity\User',
        );
    }
}

NB: The lines I commented is the only way I found to do what I want. But it does not feel the right way to do it.

In my Controller:

$currentUser = $this->container->get('security.context')->getToken()->getUser();

$followedUsers = $currentUser->getFollowedUsers(); // That works properly

$form = $this->createForm(new UserType(),$currentUser);

EDIT :

Actually my problem was that I forgot some annotation in my ManyToMany declaration. Here is the default annotation which should be used for an unidirectionnal ManyToMany relation:

/**
 * @ManyToMany(targetEntity="Group")
 * @JoinTable(name="users_groups",
 *      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
 *      )
 */

Solution was found in the doctrine documentation here : doctrine2 unidirectionnal ManyToMany.

IppX
  • 305
  • 1
  • 13

1 Answers1

0

If specified, this is used to query the subset of options (and their order) that should be used for the field. The value of this option can either be a QueryBuilder object or a Closure. If using a Closure, it should take a single argument, which is the EntityRepository of the entity.

Without specifying query_builder Symfony 2 option you'll get all FollowedUser, as you said. The meaning of:

 $builder->add('followedUsers');

Is something like:

  1. Add a field whose property is followedUsers of the User class.
  2. Guess it's type (entity type).
  3. query_builder option not specified? Then fetch all users.
  4. Select (depending of expanded and multiple options) those (options) users actually following the user from the model, leaving all other (options) users unselected.

So, question for you is: why you want to display only the users following the user in the form model? It's a no sense... the actual user of the application will never be able to add new following users.

gremo
  • 47,186
  • 75
  • 257
  • 421
  • Thx for your answer. Actually I don't want the user to be able to add new following users. I want to display a unique ` – IppX Aug 07 '12 at 20:32