Symfony 3 / Doctrine 多对一总是 returns 最后添加 - FOSUser/OAuth 扩展

Symfony 3 / Doctrine many-to-one always returns just last addition- FOSUser/OAuth extended

我正在开发一个登录页面,应该可以在其中使用不同的社交网络登录名(facebook、twitter、google 等)进行登录。 它应该像这样工作:每个用户都有可能添加 he/she 想要的社交网络登录名,所以我想将它作为一个用户 -> 许多社交网络登录名关系。 什么有效:数据存储在数据库中,但在行中 var_dump($user->getSocialnetworks()); 我只得到一个对象,而不是所有应该与该用户相关的对象。我知道函数 justATest 只是为用户 1 添加了一个社交网络登录,这只是一个测试。

<?php
namespace AppBundle\Entity\Registration;

use Doctrine\Common\Collections\ArrayCollection;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\Security\Core\User\UserInterface;

class FOSUBUserProvider extends BaseClass
{
public function justATest(UserResponseInterface $response)
    {
        // get user_id and socialnetworkname from response
        $userIdInSocialNetwork = $response->getUsername();
        $socialnetwork = $response->getResourceOwner()->getName();
        $login= new UserInSocialNetworks();
        $login->setSocialIdentyfier($response->getEmail());
        $login->setSocialNetworkSlug($socialnetwork);

        $user = $this->userManager->findUserBy(array(
                'id' => 1)
        );
        $login->setUserId($user);
        $user->addSocialNetwork($login);
        $this->userManager->updateUser($user);
        $users= $this->userManager->findUsers();
        var_dump($users);
        var_dump($socialnetwork);
        var_dump($user->getSocialnetworks());
        die();
} }

用户Class:

<?php

/*
 * This is the User class, depending on fos_userBundle
 */

namespace AppBundle\Entity\Registration;

use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="UserInSocialNetworks", mappedBy="user_id", cascade={"persist", "remove"})
     * @var ArrayCollection|null

     */
    private $socialnetworks;

    /**
     * @var string
     *
     * @ORM\Column(name="family_name",type="string", length=255, nullable=true)
     */
    private $familyName;

    /**
     * @var string
     *
     * @ORM\Column(name="given_name",type="string", length=255, nullable=true)
     */
    private $givenName;

    public function __construct()
    {
        parent::__construct();
        $this->socialnetworks = new ArrayCollection();
    }

    /**
     * @return string
     */
    public function getFamilyName()
    {
        return $this->familyName;
    }

    /**
     * @param string $familyName
     */
    public function setFamilyName($familyName)
    {
        $this->familyName = $familyName;
    }

    /**
     * @return string
     */
    public function getGivenName()
    {
        return $this->givenName;
    }

    /**
     * @param string $givenName
     */
    public function setGivenName($givenName)
    {
        $this->givenName = $givenName;
    }

    /**
     * @return ArrayCollection
     */
    public function getSocialnetworks()
    {
        return $this->socialnetworks;
    }

    /**
     * @param Collection UserInSocialNetworks
     */
    public function setSocialnetworks($socialnetworks)
    {
        $this->socialnetworks = $socialnetworks;
    }

    /**
     * @param UserInSocialNetwork
     */
    public function addSocialNetwork($socialnetwork)
    {
        $this->getSocialnetworks()->add($socialnetwork);
    }



}

存储用户社交网络的Class:

<?php

namespace AppBundle\Entity\Registration;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;



/**
 * UserInSocialNetworks
 *
 * @ORM\Table(name="user_in_social_networks")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\Registration\UserInSocialNetworksRepository")
 */
class UserInSocialNetworks
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="socialnetworks")
     * @ORM\JoinColumn(name="user_id")
     * @var User
     *
     */
    private $userId;

    /**
     * @var int
     *
     * @ORM\Column(name="social_network_slug", type="string", length=255, nullable=true)
     */
    private $socialNetworkSlug;

    /**
     * @var string
     *
     * @ORM\Column(name="social_identifier", type="string", length=255, nullable=true)
     */
    private $socialIdentyfier;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set userId
     *
     * @param integer $userId
     *
     * @return UserInSocialNetworks
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;

        return $this;
    }

    /**
     * Get userId
     *
     * @return int
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * @return int
     */
    public function getSocialNetworkSlug()
    {
        return $this->socialNetworkSlug;
    }

    /**
     * @param int $socialNetworkSlug
     */
    public function setSocialNetworkSlug($socialNetworkSlug)
    {
        $this->socialNetworkSlug = $socialNetworkSlug;
    }

    /**
     * @return string
     */
    public function getSocialIdentyfier()
    {
        return $this->socialIdentyfier;
    }

    /**
     * @param string $socialIdentyfier
     */
    public function setSocialIdentyfier($socialIdentyfier)
    {
        $this->socialIdentyfier = $socialIdentyfier;
    }



}

这是我得到的

object(Doctrine\ORM\PersistentCollection)[545]
  private 'snapshot' => 
    array (size=1) <------- just one
      0 => 
        object(AppBundle\Entity\Registration\UserInSocialNetworks)[336]
          private 'id' => int 5 <---- I added already 5 line with my code to database
          private 'userId' => 
            object(AppBundle\Entity\Registration\User)[516]
              ...
          private 'socialNetworkSlug' => string 'google' (length=6)
          private 'socialIdentyfier' => string 'blablabla@googlemail.com' (length=24)

对不起,我的错, 解决方案是纠正关系:

class UserInSocialNetworks
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * Many Socialnetwork Logins have one User
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Registration\User", inversedBy="socialnetworks")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     *
     */
    private $user;

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

    /**
     * One User can have many social networks
     * @ORM\OneToMany(targetEntity="UserInSocialNetworks", mappedBy="user", cascade={"remove"})
     */
    private $socialnetworks;