0

我有一个成员实体和一个朋友实体。我通过加入表格来获得成员的所有朋友。我通过 加入朋友的成员member_id

表结构:

成员 member_id, full_name, email, password, date_joined

朋友们 friendship_id, member_id, friend_id

一切都很好,但我无法full_name获得friend_id. 我运行命令行来生成实体中的 getter、setter 和连接信息。

在 twig 中,我从控制器中获取了 friends 数组并调用friend.fullName返回我的名字,而不是我的朋友的名字,但我加入了member_id,所以这很好。

我需要在没有 2 个查询的情况下获取fullNameid的memberfriend_id

前任。

member_id: 1, full_name:John

朋友 member_id: 22, full_name:Billy

我可以打电话friend.friendId,它会返回 22,我很难得到fullName

会员单位

<?php

namespace Pixbelly\AppBundle\Entity;

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

/**
* Pixbelly\AppBundle\Entity\Members
*
* @ORM\Table(name="members")
* @ORM\Entity(repositoryClass="Pixbelly\AppBundle\Repository\MembersRepository")
 */
class Members
{
/**
 * @var integer $memberId
 *
 * @ORM\Column(name="member_id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $memberId;

/**
 * @var string $fullName
 *
 * @ORM\Column(name="full_name", type="string", length=255, nullable=false)
 */
private $fullName;

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

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

/**
 * @var \DateTime $dateJoined
 *
 * @ORM\Column(name="date_joined", type="datetime", nullable=false)
 */
private $dateJoined;

/**
 * @ORM\OneToMany(targetEntity="Friends", mappedBy="member")
 */
protected $friends;

public function __construct()
{
    $this->friends = new ArrayCollection();
}

/**
 * Get memberId
 *
 * @return integer 
 */
public function getMemberId()
{
    return $this->memberId;
}

/**
 * Set fullName
 *
 * @param string $fullName
 * @return Members
 */
public function setFullName($fullName)
{
    $this->fullName = $fullName;

    return $this;
}

/**
 * Get fullName
 *
 * @return string 
 */
public function getFullName()
{
    return $this->fullName;
}

/**
 * Set email
 *
 * @param string $email
 * @return Members
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

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

/**
 * Set password
 *
 * @param string $password
 * @return Members
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string 
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Set dateJoined
 *
 * @param \DateTime $dateJoined
 * @return Members
 */
public function setDateJoined($dateJoined)
{
    $this->dateJoined = $dateJoined;

    return $this;
}

/**
 * Get dateJoined
 *
 * @return \DateTime 
 */
public function getDateJoined()
{
    return $this->dateJoined;
}

/**
 * Add friends
 *
 * @param Pixbelly\AppBundle\Entity\Friends $friends
 * @return Members
 */
public function addFriend(\Pixbelly\AppBundle\Entity\Friends $friends)
{
    $this->friends[] = $friends;

    return $this;
}

/**
 * Remove friends
 *
 * @param Pixbelly\AppBundle\Entity\Friends $friends
 */
public function removeFriend(\Pixbelly\AppBundle\Entity\Friends $friends)
{
    $this->friends->removeElement($friends);
}

/**
 * Get friends
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getFriends()
{
    return $this->friends;
}
}

朋友实体

<?php

namespace Pixbelly\AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Pixbelly\AppBundle\Entity\Friends
 *
 * @ORM\Table(name="friends")
 * @ORM\Entity(repositoryClass="Pixbelly\AppBundle\Repository\FriendsRepository")
 */
class Friends
{
 /**
 * @var integer $id
 *
 * @ORM\Column(name="friendship_id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $friendshipId;

/**
 * @var integer $memberId
 *
 * @ORM\Column(name="member_id", type="integer", nullable=false)
 */
private $memberId;

/**
 * @var integer $friendId
 *
 * @ORM\Column(name="friend_id", type="integer", nullable=false)
 */
private $friendId;

/**
 * @ORM\ManyToOne(targetEntity="Members", inversedBy="friends")
 * @ORM\JoinColumn(name="member_id", referencedColumnName="member_id")
 */
protected $member;

/**
 * Get friendshipId
 *
 * @return integer 
 */
public function getFriendshipId()
{
    return $this->friendshipId;
}

/**
 * Set memberId
 *
 * @param integer $memberId
 * @return Friends
 */
public function setMemberId($memberId)
{
    $this->memberId = $memberId;

    return $this;
}

/**
 * Get memberId
 *
 * @return integer 
 */
public function getMemberId()
{
    return $this->memberId;
}

/**
 * Set friendId
 *
 * @param integer $friendId
 * @return Friends
 */
public function setFriendId($friendId)
{
    $this->friendId = $friendId;

    return $this;
}

/**
 * Get friendId
 *
 * @return integer 
 */
public function getFriendId()
{
    return $this->friendId;
}

/**
 * Set member
 *
 * @param Pixbelly\AppBundle\Entity\Members $member
 * @return Friends
 */
public function setMember(\Pixbelly\AppBundle\Entity\Members $member = null)
{
    $this->member = $member;

    return $this;
}

/**
 * Get member
 *
 * @return Pixbelly\AppBundle\Entity\Members 
 */
public function getMember()
{
    return $this->member;
}
}
4

0 回答 0