首页 > 解决方案 > 获取空值以访问学说存储库中的数据

问题描述

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

我也尝试过从 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
}
]

标签: symfonydoctrine-ormdoctrine

解决方案


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

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

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

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

推荐阅读