2

我是 symfony 的新手,我环顾四周,但没有找到问题的正确答案。

我有两个与多对多关系链接的实体。实体User-> 实体FollowedUser。一个User应该可以跟随几个FollowedUser,一个FollowedUser应该有几个Users跟随他。

我的问题是,当我尝试FollowedUser为一个列出所有内容时User,比如说 my CurrentUser,我得到FollowedUser的不仅仅是那些与 my 相关联的所有内容CurrentUser

这是我的代码。

用户实体( 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;
     }
}

用户类型

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',
        );
    }
}

注意:我评论的行是我发现做我想做的唯一方法。但感觉不是正确的方法。

在我的控制器中:

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

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

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

编辑 :

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.

4

1 回答 1

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.

于 2012-08-06T20:07:44.080 回答