使用连接时未获取第二个表中的Doctrine值

时间:2016-02-01 09:50:15

标签: php mysql doctrine-orm

我正在使用以下查询来获取记录

      $qb = $EM->createQueryBuilder();
                $PostDataSets = $qb->select('user','follower')->from('WebService\Entity\User', 'user')
                        ->innerJoin('WebService\Entity\Follower','follower', $qb->expr()->eq('user.id', 'follower.following_id'))
                        ->where($qb->expr()->like('user.username', $qb->expr()->literal('%' . 'admin' . '%')))
                        ->getQuery()
                        ->getResult();

现在,当我遍历结果集时,我得到以下错误

  foreach ($PostDataSets as $i => $PostDataSet) {
                $postDataArray[$i]['id'] = $PostDataSet->getId();
                $postDataArray[$i]['user_id'] = $PostDataSet->getId();
                $postDataArray[$i]['username'] = $PostDataSet->getUserName();
                $postDataArray[$i]['full_name'] = $PostDataSet->getFullName();
                $postDataArray[$i]['photo'] = $PostDataSet->getPhoto();
                $postDataArray[$i]['created_at'] = $PostDataSet->getCreatedAt();
                $postDataArray[$i]['updated_at'] = $PostDataSet->getUpdatedAt();
            }

这是我得到的错误

Fatal error: Call to undefined method WebService\Entity\Follower::getUserName() in R:\xampp\htdocs\myapp\module\WebService\src\WebService\Controller\PostController.php on line 142

但如果我将查询更改为以下内容,我就不会收到任何错误。

 $qb = $EM->createQueryBuilder();
                $PostDataSets = $qb->select('user')->from('WebService\Entity\User', 'user')
                        ->innerJoin('WebService\Entity\Follower','follower', $qb->expr()->eq('user.id', 'follower.following_id'))
                        ->where($qb->expr()->like('user.username', $qb->expr()->literal('%' . 'admin' . '%')))
                        ->getQuery()
                        ->getResult();

任何1可以解释错误的原因吗?

修改 这是转储

object(stdClass)#365 (34) { ["__CLASS__"]=> string(22) "WebService\Entity\User" ["id"]=> int(1) ["Country"]=> NULL ["Posts"]=> array(17) { [0]=> string(22) "WebService\Entity\Post" [1]=> string(22) "WebService\Entity\Post" [2]=> string(22) "WebService\Entity\Post" [3]=> string(22) "WebService\Entity\Post" [4]=> string(22) "WebService\Entity\Post" [5]=> string(22) "WebService\Entity\Post" [6]=> string(22) "WebService\Entity\Post" [7]=> string(22) "WebService\Entity\Post" [8]=> string(22) "WebService\Entity\Post" [9]=> string(22) "WebService\Entity\Post" [10]=> string(22) "WebService\Entity\Post" [11]=> string(22) "WebService\Entity\Post" [12]=> string(22) "WebService\Entity\Post" [13]=> string(22) "WebService\Entity\Post" [14]=> string(22) "WebService\Entity\Post" [15]=> string(22) "WebService\Entity\Post" [16]=> string(22) "WebService\Entity\Post" } ["FollowingUsers"]=> array(4) { [0]=> string(26) "WebService\Entity\Follower" [1]=> string(26) "WebService\Entity\Follower" [2]=> string(26) "WebService\Entity\Follower" [3]=> string(26) "WebService\Entity\Follower" } ["FollowerUsers"]=> array(4) { [0]=> string(26) "WebService\Entity\Follower" [1]=> string(26) "WebService\Entity\Follower" [2]=> string(26) "WebService\Entity\Follower" [3]=> string(26) "WebService\Entity\Follower" } ["PostLikes"]=> array(0) { } ["CommentLikes"]=> array(0) { } ["Comments"]=> array(1) { [0]=> string(25) "WebService\Entity\Comment" } ["username"]=> string(5) "admin" ["password"]=> string(60) "$2y$10$WxVcjXy3ond8dyYKz3VeQuv155pLWcNYoSti5hSB31oEHhWjWI3DC" ["email"]=> string(21) "m.rashid.se@gmail.com" ["fname"]=> NULL ["lname"]=> NULL ["full_name"]=> string(15) "Muhammad Rashid" ["dname"]=> NULL ["dob"]=> object(stdClass)#850 (3) { ["__CLASS__"]=> string(8) "DateTime" ["date"]=> string(25) "1983-06-08T00:00:00+02:00" ["timezone"]=> string(13) "Europe/Berlin" } ["gender"]=> NULL ["relationship"]=> NULL ["interested_in"]=> NULL ["photo"]=> NULL ["cover_photo"]=> NULL ["state"]=> NULL ["city"]=> NULL ["address"]=> NULL ["current_location"]=> NULL ["cell"]=> NULL ["phone"]=> NULL ["about_me"]=> NULL ["remember_me"]=> NULL ["salt"]=> string(24) "WxVcjXy3ond8dyYKz3VeQw==" ["created_at"]=> object(stdClass)#851 (3) { ["__CLASS__"]=> string(8) "DateTime" ["date"]=> string(25) "2014-12-20T13:31:33+01:00" ["timezone"]=> string(13) "Europe/Berlin" } ["updated_at"]=> NULL ["deleted_at"]=> NULL } object(stdClass)#365 (7) { ["__CLASS__"]=> string(26) "WebService\Entity\Follower" ["id"]=> int(1) ["FollowingUser"]=> object(stdClass)#660 (34) { ["__CLASS__"]=> string(22) "WebService\Entity\User" ["id"]=> int(1) ["Country"]=> NULL ["Posts"]=> string(9) "Array(17)" ["FollowingUsers"]=> string(8) "Array(4)" ["FollowerUsers"]=> string(8) "Array(4)" ["PostLikes"]=> string(8) "Array(0)" ["CommentLikes"]=> string(8) "Array(0)" ["Comments"]=> string(8) "Array(1)" ["username"]=> string(5) "admin" ["password"]=> string(60) "$2y$10$WxVcjXy3ond8dyYKz3VeQuv155pLWcNYoSti5hSB31oEHhWjWI3DC" ["email"]=> string(21) "m.rashid.se@gmail.com" ["fname"]=> NULL ["lname"]=> NULL ["full_name"]=> string(15) "Muhammad Rashid" ["dname"]=> NULL ["dob"]=> string(8) "DateTime" ["gender"]=> NULL ["relationship"]=> NULL ["interested_in"]=> NULL ["photo"]=> NULL ["cover_photo"]=> NULL ["state"]=> NULL ["city"]=> NULL ["address"]=> NULL ["current_location"]=> NULL ["cell"]=> NULL ["phone"]=> NULL ["about_me"]=> NULL ["remember_me"]=> NULL ["salt"]=> string(24) "WxVcjXy3ond8dyYKz3VeQw==" ["created_at"]=> string(8) "DateTime" ["updated_at"]=> NULL ["deleted_at"]=> NULL } ["FollowerUser"]=> object(stdClass)#864 (34) { ["__CLASS__"]=> string(22) "WebService\Entity\User" ["id"]=> int(1) ["Country"]=> NULL ["Posts"]=> string(9) "Array(17)" ["FollowingUsers"]=> string(8) "Array(4)" ["FollowerUsers"]=> string(8) "Array(4)" ["PostLikes"]=> string(8) "Array(0)" ["CommentLikes"]=> string(8) "Array(0)" ["Comments"]=> string(8) "Array(1)" ["username"]=> string(5) "admin" ["password"]=> string(60) "$2y$10$WxVcjXy3ond8dyYKz3VeQuv155pLWcNYoSti5hSB31oEHhWjWI3DC" ["email"]=> string(21) "m.rashid.se@gmail.com" ["fname"]=> NULL ["lname"]=> NULL ["full_name"]=> string(15) "Muhammad Rashid" ["dname"]=> NULL ["dob"]=> string(8) "DateTime" ["gender"]=> NULL ["relationship"]=> NULL ["interested_in"]=> NULL ["photo"]=> NULL ["cover_photo"]=> NULL ["state"]=> NULL ["city"]=> NULL ["address"]=> NULL ["current_location"]=> NULL ["cell"]=> NULL ["phone"]=> NULL ["about_me"]=> NULL ["remember_me"]=> NULL ["salt"]=> string(24) "WxVcjXy3ond8dyYKz3VeQw==" ["created_at"]=> string(8) "DateTime" ["updated_at"]=> NULL ["deleted_at"]=> NULL } ["are_we_following"]=> int(1) ["created_at"]=> NULL ["updated_at"]=> NULL }
Fatal error: Call to undefined method WebService\Entity\Follower::getUserName() in R:\xampp\htdocs\kanvas-php\module\WebService\src\WebService\Controller\PostController.php on line 144


<?php

namespace WebService\Entity;

use Doctrine\ORM\Mapping as ORM;
use WebService\Entity\Post;
use WebService\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
/**
 * An Follower entity.
 * 
 * @ORM\Entity
 * @ORM\Table(name="followers")
 * 
 * @property int $id
 * @property User $FollowingUser
 * @property User $FollowerUser
 * @property smallint $are_we_following
 * @property datetime $created_at
 * @property datetime $updated_at
 * 
 */
class Follower extends EntityHydrator {

    /**
     * @ORM\Id 
     * @ORM\Column(type="integer",length=11);
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * This is the user who will be followed
     * @ORM\ManyToOne(targetEntity="User", inversedBy="FollowingUsers")
     * @ORM\JoinColumn(name="following_id", referencedColumnName="id", nullable=false)
     * 
     */
    protected $FollowingUser;

    /**
     * This is the user who is going to follow
     * @ORM\ManyToOne(targetEntity="User", inversedBy="FollowerUsers")
     * @ORM\JoinColumn(name="follower_id", referencedColumnName="id", nullable=false)
     * 
     */
    protected $FollowerUser;

    /**
     * This column shows that are we also following the user
     * @ORM\Column(type="smallint", length=1, nullable=true, options={"default"=0})
     * 
     */
    protected $are_we_following;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * 
     */
    protected $created_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * 
     */
    protected $updated_at;

    public function  __construct(array $data = null) {
        parent::__construct($data, $this, true);
    }

    public function  getId() {
        return $this->id;
    }

    public function getFollowingUser() {
        return $this->FollowingUser;
    }

    public function getFollowerUser() {
        return $this->FollowerUser;
    }

    public function getAreWeFollowing() {
        return $this->are_we_following;
    }

    public function getCreatedAt() {
        return $this->created_at;
    }

    public function getUpdatedAt() {
        return $this->updated_at;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setFollowingUser($FollowingUser) {
        $this->FollowingUser = $FollowingUser;
    }

    public function setFollowerUser($FollowerUser) {
        $this->FollowerUser = $FollowerUser;
    }

    public function setAreWeFollowing($are_we_following) {
        $this->are_we_following = $are_we_following;
    }

    public function setCreatedAt($created_at) {
        $this->created_at = $created_at;
    }

    public function setUpdatedAt($updated_at) {
        $this->updated_at = $updated_at;
    }
}

用户实体

<?php

namespace WebService\Entity;

use Doctrine\ORM\Mapping as ORM;
use \WebService\Entity\Country;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * An User entity.
 * 
 * @ORM\Entity
 * @ORM\Table(name="users", uniqueConstraints={@ORM\UniqueConstraint(name="username", columns={"username"}), @ORM\UniqueConstraint(name="email", columns={"email"})})
 * 
 * @property int $id
 * @property Country $Country
 * @property ArrayCollection $Posts
 * @property ArrayCollection $Comments
 * @property ArrayCollection $CommentLikes
 * @property ArrayCollection $FollowingUsers
 * @property ArrayCollection $FollowerUsers
 * @property string $username
 * @property string $password 
 * @property string $email 
 * @property string $fname
 * @property string $lname
 * @property string $dname
 * @property string $dob
 * @property string $gender
 * @property string $relationship
 * @property string $interested_in
 * @property string $photo
 * @property string $cover_photo
 * @property string $country_id
 * @property string $state
 * @property string $city
 * @property string $address
 * @property string $current_location
 * @property string $cell
 * @property string $phone
 * @property string $about_me
 * @property string $remember_me
 * @property string $salt
 * @property datetime $created_at
 * @property datetime $updated_at
 * @property datetime $deleted_at
 * 
 */
class User extends EntityHydrator{

    const SALT = 'ACK3HtAz[|U8]Of9@?Ppj:!';
    const SALT_LENGTH = 16;

    /**
     * @ORM\Id 
     * @ORM\Column(type="integer",length=11);
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Country", inversedBy="Users")
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
     * 
     */
    protected $Country;

    /**
     * @ORM\OneToMany(targetEntity="Post", mappedBy="User", orphanRemoval=true, cascade={"persist"})
     * 
     */
    protected $Posts;

    /**
     * @ORM\OneToMany(targetEntity="Follower", mappedBy="FollowingUser", orphanRemoval=true, cascade={"persist"})
     * 
     */
    protected $FollowingUsers;

    /**
     * @ORM\OneToMany(targetEntity="Follower", mappedBy="FollowerUser", orphanRemoval=true, cascade={"persist"})
     * 
     */
    protected $FollowerUsers;

    /**
     * @ORM\OneToMany(targetEntity="PostLike", mappedBy="User", orphanRemoval=true, cascade={"persist"})
     *
     */
    protected $PostLikes;

    /**
     * @ORM\OneToMany(targetEntity="CommentLike", mappedBy="User", orphanRemoval=true, cascade={"persist"})
     *
     */
    protected $CommentLikes;

    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="User", orphanRemoval=true, cascade={"persist"})
     *
     */
    protected $Comments;

    /**
     * @ORM\Column(type="string", length=50, nullable=false)
     * 
     */
    protected $username;

    /**
     * @ORM\Column(type="string", length=255, nullable=false)
     * 
     */
    protected $password;

    /**
     * @ORM\Column(type="string", length=60, nullable=false)
     * 
     */
    protected $email;

    /**
     * @ORM\Column(type="string", length=60, nullable=true)
     * 
     */
    protected $fname;

    /**
     * @ORM\Column(type="string", length=60, nullable=true)
     *
     */
    protected $lname;

    /**
     * @ORM\Column(type="string", length=120, nullable=false)
     *
     */
    protected $full_name;

    /**
     * @ORM\Column(type="string", length=60, nullable=true)
     *
     */
    protected $dname;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * 
     */
    protected $dob;

    /**
     * @ORM\Column(type="string", columnDefinition="ENUM('Male','Female', 'Do not wish to disclose')", nullable=true)
     * 
     */
    protected $gender;

    /**
     * @ORM\Column(type="string", columnDefinition="ENUM('Married','Single', 'Complicated','Divorced','Seperated','Engaged')", nullable=true)
     * 
     */
    protected $relationship;

    /**
     * @ORM\Column(type="string", columnDefinition="ENUM('Male','Female', 'Both')", nullable=true)
     * 
     */
    protected $interested_in;

    /**
     * @ORM\Column(type="string",length=1024,  nullable=true)
     * 
     */
    protected $photo;

    /**
     * @ORM\Column(type="string",length=1024,  nullable=true)
     * 
     */
    protected $cover_photo;

    /**
     * @ORM\Column(type="string",length=60,  nullable=true)
     * 
     */
    protected $state;

    /**
     * @ORM\Column(type="string",length=60,  nullable=true)
     * 
     */
    protected $city;

    /**
     * @ORM\Column(type="string",length=255,  nullable=true)
     * 
     */
    protected $address;

    /**
     * @ORM\Column(type="string",length=255,  nullable=true)
     * 
     */
    protected $current_location;

    /**
     * @ORM\Column(type="string",length=20,  nullable=true)
     * 
     */
    protected $cell;

    /**
     * @ORM\Column(type="string",length=20,  nullable=true)
     * 
     */
    protected $phone;

    /**
     * @ORM\Column(type="string",length=255,  nullable=true)
     * 
     */
    protected $about_me;

    /**
     * @ORM\Column(type="smallint", length=1, nullable=true, options={"default"=0})
     * 
     */
    protected $remember_me;

    /**
     * @ORM\Column(type="string", length=100, nullable=false)
     * 
     */
    protected $salt;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * 
     */
    protected $created_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * 
     */
    protected $updated_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * 
     */
    protected $deleted_at;


    public function __construct(array $data = null)
    {
        parent::__construct($data, $this, true);
    }

    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
        return $this;
    }

    public function getUsername() {
        return $this->username;
    }

    public function setUsername($username) {
        $this->username = $username;
        return $this;
    }

    public function getPassword() {
        return $this->password;
    }

    public function setPassword($password) {
        $Bcrypt = new \Zend\Crypt\Password\Bcrypt();
        if (empty($this->salt)) {
            $this->setSalt();
        }
        $Bcrypt->setSalt(base64_decode($this->salt));
        $hashedPassword = $Bcrypt->create($password);
        $this->password = $hashedPassword;
//        $this->password = $password;
    }

    public function getSalt() {
        return $this->salt;
    }

    public function setSalt($salt = null) {
        if (empty($salt)) {
            $salt = \Zend\Math\Rand::getBytes(self::SALT_LENGTH, $strong = false);
            $encrytSalt = base64_encode($salt);
        } else {
            $encrytSalt = $salt;
        }
        $this->salt = $encrytSalt;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getComments()
    {
        return $this->Comments;
    }

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

    /**
     * @return mixed
     */
    public function getCountry()
    {
        return $this->Country;
    }

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

    /**
     * @return mixed
     */
    public function getPosts()
    {
        return $this->Posts;
    }

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

    /**
     * @return mixed
     */
    public function getAboutMe()
    {
        return $this->about_me;
    }

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

    /**
     * @return mixed
     */
    public function getAddress()
    {
        return $this->address;
    }

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

    /**
     * @return mixed
     */
    public function getCell()
    {
        return $this->cell;
    }

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

    /**
     * @return mixed
     */
    public function getCity()
    {
        return $this->city;
    }

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

    /**
     * @return mixed
     */
    public function getCountryId()
    {
        return $this->country_id;
    }

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

    /**
     * @return mixed
     */
    public function getCoverPhoto()
    {
        return $this->cover_photo;
    }

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

    /**
     * @return mixed
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

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

    /**
     * @return mixed
     */
    public function getCurrentLocation()
    {
        return $this->current_location;
    }

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

    /**
     * @return mixed
     */
    public function getDeletedAt()
    {
        return $this->deleted_at;
    }

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

    /**
     * @return mixed
     */
    public function getDname()
    {
        return $this->dname;
    }

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

    /**
     * @return mixed
     */
    public function getDob()
    {
        return $this->dob;
    }

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

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this->email;
    }

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

    /**
     * @return mixed
     */
    public function getFname()
    {
        return $this->fname;
    }

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

    /**
     * @return mixed
     */
    public function getFullName()
    {
        return $this->full_name;
    }

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

    /**
     * @return mixed
     */
    public function getGender()
    {
        return $this->gender;
    }

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

    /**
     * @return mixed
     */
    public function getInterestedIn()
    {
        return $this->interested_in;
    }

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

    /**
     * @return mixed
     */
    public function getLname()
    {
        return $this->lname;
    }

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

    /**
     * @return mixed
     */
    public function getPhone()
    {
        return $this->phone;
    }

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

    /**
     * @return mixed
     */
    public function getPhoto()
    {
        return $this->photo;
    }

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

    /**
     * @return mixed
     */
    public function getRelationship()
    {
        return $this->relationship;
    }

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

    /**
     * @return mixed
     */
    public function getRememberMe()
    {
        return $this->remember_me;
    }

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

    /**
     * @return mixed
     */
    public function getState()
    {
        return $this->state;
    }

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

    /**
     * @return mixed
     */
    public function getUpdatedAt()
    {
        return $this->updated_at;
    }

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

    //============================================================================


    public static function verifyHashedPassword(\WebService\Entity\User $User, $passwordGiven) {
        $Bcrypt = new \Zend\Crypt\Password\Bcrypt();
        $Bcrypt->setSalt(base64_decode($User->getSalt()));
        return $Bcrypt->verify($passwordGiven, $User->getPassword());
    }

    public static function makeHashedPassword(\WebService\Entity\User $User, $passwordGiven)
    {
        //@todo makeHashedPassword Definition
        return '';
    }

}

1 个答案:

答案 0 :(得分:1)

我不是100%肯定这个答案,但让我们看看:

1。)问题

您可以阅读here

  

SELECT查询可以包含联接。有两种类型的JOIN:“常规”连接和“获取”连接。

     

常规联接:用于限制结果和/或计算汇总值。

     

获取联接:除了使用常规联接之外:用于获取相关实体并将其包含在查询的水合结果中。

     

没有特殊的DQL关键字可以区分常规连接和提取连接。只要连接实体的字段出现在聚合函数之外的DQL查询的SELECT部分​​中,连接(无论是内连接还是外连接)就会成为“获取连接”。否则就是“定期加入”。

所以基本上你的第一个查询使用了一个获取连接,而你的第二个查询(它正在工作)使用一个常规连接。

现在,为什么常规联接工作而且fetch join不是? 常规连接提取用户实体,您可以按预期调用getUserName方法。跟随者关系或多或少被忽略,因为在常规联接中,它只会用于过滤结果。

获取连接变体(这里我们到达我不太确定的部分)必须获取用户跟随实体,因为它们都被传递给{{1 }} 方法。但是跟随者实体是关系的拥有方(关系存储在跟随者实体中),因此查询返回跟随者实体而不是用户实体,并且您必须通过调用某些select方法来获取User实体在追随者实体上。 这也可以解释为什么getUser方法(可能存在于两个实体上)即使在getId方法之前调用也不会导致错误。

2.)可能的解决方案

您的查询可能应该如下所示:

getUserName

这应该会在用户匹配$EM->createQueryBuilder(); $followers = $qb->select('follower', 'user') ->from('WebService\Entity\Follower', 'follower') ->join('follower.FollowingUser', 'user') ->where($qb->expr()->like('user.username', $qb->expr()->literal('%' . 'admin' . '%'))) ->getQuery() ->getResult(); foreach ($followers as $follower) { $user = $follower->getFollowingUser(); $postDataArray[$i]['id'] = $follower->getId(); $postDataArray[$i]['user_id'] = $user->getId(); $postDataArray[$i]['username'] = $user->getUserName(); $postDataArray[$i]['full_name'] = $user->getFullName(); $postDataArray[$i]['photo'] = $user->getPhoto(); $postDataArray[$i]['created_at'] = $follower->getCreatedAt(); $postDataArray[$i]['updated_at'] = $follower->getUpdatedAt(); } 之后为您提供所有关注者;虽然没经过测试。