获取空值以访问准则存储库中的数据

时间:2019-01-11 12:22:11

标签: symfony doctrine-orm doctrine

从FriendsRepo获取数据时,我获得了通过id与Friends实体连接的User实体的空值

我也尝试过从UsersRepo访问数据,但没有成功。

用户实体:

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

/**
 * @var string
 * @Assert\NotBlank()
 * @ORM\Column(name="email", type="string", length=20, unique=true)
 */
private $email;

/**
 * @var string
 * @Assert\NotBlank()
 * @ORM\Column(name="username", type="string", length=20, unique=true)
 */
private $username;

/**
 * @var string
 * @Assert\NotBlank()
 * @ORM\Column(name="password", type="string", length=255)
 */
private $password;


/**
 * @var string
 *
 * @ORM\Column(name="user_picture", type="string", nullable=true)
 *
 */
private $userPic;

/**
 * @var ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\UserPosts", mappedBy="user")
 *
 *
 */

private $userPosts;

public function __construct()
{
    $this->userPosts = new ArrayCollection();
    $this->myfriends = new ArrayCollection();
    $this->friendof = new ArrayCollection();
}


/**
 * @var
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Friends", mappedBy="afriendof")
 */

private $friendof;
/**
 * @var
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Friends", mappedBy="friendsWithMe")
 */
private $myfriends;
 /**
 * @return User
 */
public function getFriendof()
{
    return $this->friendof;
}

/**
 * @param User $friendof
 */
public function setFriendof($friendof)
{
    $this->friendof = $friendof;
}

/**
 * @return User
 */
public function getMyfriends()
{
    return $this->myfriends;
}

/**
 * @param User $myfriends
 */
public function setMyfriends($myfriends)
{
    $this->myfriends = $myfriends;
}
}

朋友实体:

class Friends
{

/**
 * @ORM\Id()
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="myfriends")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
 */

private $friendsWithMe;


/**
 * @ORM\Id()
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="friendof")
 * @ORM\JoinColumn(name="friend_id", referencedColumnName="id", nullable=false)
 */

private $afriendof;

/**
 * @var integer
 *
 * @ORM\Column(name="status", type="smallint")
 */
private $status;



/**
 * @return User
 */
public function getFriendsWithMe()
{
    return $this->friendsWithMe;
}

/**
 * @param mixed $friendsWithMe
 */
public function setFriendsWithMe($friendsWithMe)
{
    $this->friendsWithMe = $friendsWithMe;
}

/**
 * @return User
 */
public function getAfriendof()
{
    return $this->afriendof;
}

/**
 * @param mixed $afriendof
 */
public function setAfriendof($afriendof)
{
    $this->afriendof = $afriendof;
}

/**
 * @return integer
 */
public function getStatus()
{
    return $this->status;
}

/**
 * @param integer $status
 */
public function setStatus($status)
{
    $this->status = $status;
}
}

我的好友回购:

public function personalFriends($userId){
    $em = $this->getEntityManager();
    $result = $em->createQuery('SELECT friends FROM AppBundle\Entity\Friends friends WHERE friends.friendsWithMe= :userId');
    $result->setParameter('userId', $userId);
    return $result->getResult();
}

我希望获得朋友的用户名,图片的数据,但是除id之外,我在所有数据上都得到空值,我知道问题很小,但是我找不到解决方法,所以请帮忙(示例):

DefaultController.php on line 38:
array:1 [▼
  0 => Friends {#649 ▼
-friendsWithMe: User {#436 ▼
  -id: 45
  -email: "pesho1@abv.bg"
  -username: "pesho"
  -password: "$2y$13$5TYROts3shJjgZ41CNUE8.bMTG.JNwKGdzRuKMVSlySULFEAttVyK"
  -userPic: "fb9afe572dce4a0f82481c42063b727f.jpeg"
  -userPosts: PersistentCollection {#564 ▶}
  -friendof: PersistentCollection {#412 ▶}
  -myfriends: PersistentCollection {#414 ▶}
  -roles: "ROLE_ADMIN"
  -isActive: null
}
-afriendof: User {#596 ▼
  +__isInitialized__: false
  -id: 12
  -email: null
  -username: null
  -password: null
  -userPic: null
  -userPosts: null
  -friendof: null
  -myfriends: null
  -roles: null
  -isActive: null
   …2
}
-status: 0
}
]

1 个答案:

答案 0 :(得分:3)

那是因为默认情况下启用了延迟加载。您需要显式加入用户表。

$result = $em->createQuery('SELECT friends, user FROM AppBundle\Entity\Friends friends JOIN AppBundle\Entity\User user WHERE friends.friendsWithMe= :userId');

这样,将立即获取数据。否则,将在您首次尝试访问数据时获取数据,即

$friend->getAFriendOf()->getEmail();