我正在尝试使用Doctrine 2 ORM在User和Team模型之间建立多对多关系。
当我手动查询时,我可以在我的连接表上看到数据库中的条目,但似乎Doctrine没有加载任何相关实体。
我正在创建这样的实体;
$firstTeam = new Bere\Models\Team;
$firstTeam->setTitle("First Team");
$em->persist($firstTeam);
$firstUser = new Bere\Models\User;
$firstUser->setFirstName("aa");
$firstUser->setMiddleName("ss");
$firstUser->setLastName("dd");
$firstUser->setEmail("bb");
$firstUser->setPhone("qq");
$em->persist($firstUser);
$em->flush();
$firstTeam->addUser($firstUser);
$em->persist($firstTeam);
$em->flush();
然后像这样查询;
$u = $em->getRepository(Bere\Models\User::class)->findOneBy(array(
'email' => 'XXXXXXX'
))->toNamedArray();
输出:
{
"id": "xx",
"email": "aaa",
"phone": "bb",
"firstName": "qq",
"middleName": "ww",
"lastName": "ee",
"permissionGroups": {},
"permissions": {},
"teams": [
{}
]
}
Team.php
/**
* Class Team
* @ORM\Entity
* @package Bere\Models
*/
class Team implements IJsonModel
{
/**
* @var string
* @ORM\Id
* @ORM\Column(type="string")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="\Bere\Utils\UUIDGenerator")
*/
protected $id;
/**
* @ORM\Column
*/
protected $title;
/**
* @var Collection|User[]
* @ORM\ManyToMany(targetEntity="User", inversedBy="teams", cascade={"all"}, fetch="EAGER")
*/
protected $users;
/**
* Team constructor.
*/
public function __construct()
{
$this->users = new ArrayCollection();
}
public function addUser(User $user)
{
if ($this->users->contains($user))
return;
$this->users->add($user);
$user->addTeam($this);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @param string $id
*/
public function setId(string $id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* @param mixed $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return mixed
*/
public function getUsers()
{
return $this->users->toArray();
}
/**
* @param Collection $users
*/
public function setUsers(Collection $users)
{
$this->users = $users;
}
public function toNamedArray()
{
return array(
"id" => $this->getId(),
"title" => $this->getTitle(),
"users" => $this->getUsers()
);
}
}
user.php的
namespace Bere\Models;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* Class User
* @ORM\Entity
* @package Bere\Models
*/
class User implements IJsonModel
{
/**
* @var string
* @ORM\Id
* @ORM\Column(type="string")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="\Bere\Utils\UUIDGenerator")
*/
protected $id;
/**
* @ORM\Column
*/
protected $email;
/**
* @ORM\Column
*/
protected $phone;
/**
* @ORM\Column
*/
protected $firstName;
/**
* @ORM\Column
*/
protected $middleName;
/**
* @ORM\Column
*/
protected $lastName;
/**
* @var Collection
* @ORM\ManyToMany(targetEntity="PermissionGroup")
* @ORM\JoinTable(name="user_permission_groups",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="permission_group_id", referencedColumnName="id")}
* )
*/
protected $permissionGroups;
/**
* @var Collection
* @ORM\ManyToMany(targetEntity="Permission")
* @ORM\JoinTable(name="user_permissions",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="permission_id", referencedColumnName="id")}
* )
*/
protected $permissions;
/**
* @var Collection|Team[]
* @ORM\ManyToMany(targetEntity="Team", mappedBy="users", cascade={"all"}, fetch="EAGER")
*/
protected $teams;
/**
* User constructor.
*/
public function __construct()
{
$this->permissionGroups = new ArrayCollection();
$this->permissions = new ArrayCollection();
$this->teams = new ArrayCollection();
}
public function addTeam(Team $team)
{
if ($this->teams->contains($team))
return;
$this->teams->add($team);
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @param string $id
*/
public function setId(string $id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param mixed $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return mixed
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param mixed $phone
*/
public function setPhone($phone)
{
$this->phone = $phone;
}
/**
* @return mixed
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* @param mixed $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
/**
* @return mixed
*/
public function getMiddleName()
{
return $this->middleName;
}
/**
* @param mixed $middleName
*/
public function setMiddleName($middleName)
{
$this->middleName = $middleName;
}
/**
* @return mixed
*/
public function getLastName()
{
return $this->lastName;
}
/**
* @param mixed $lastName
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
/**
* @return Collection
*/
public function getPermissionGroups(): Collection
{
return $this->permissionGroups;
}
/**
* @param Collection $permissionGroups
*/
public function setPermissionGroups(Collection $permissionGroups)
{
$this->permissionGroups = $permissionGroups;
}
/**
* @return Collection
*/
public function getPermissions(): Collection
{
return $this->permissions;
}
/**
* @param Collection $permissions
*/
public function setPermissions(Collection $permissions)
{
$this->permissions = $permissions;
}
/**
* @return mixed
*/
public function getTeams()
{
return $this->teams->toArray();
}
/**
* @param mixed $teams
*/
public function setTeams($teams)
{
$this->teams = $teams;
}
public function toNamedArray()
{
return array(
"id" => $this->getId(),
"email" => $this->getEmail(),
"phone" => $this->getPhone(),
"firstName" => $this->getFirstName(),
"middleName" => $this->getMiddleName(),
"lastName" => $this->getLastName(),
"permissionGroups" => $this->getPermissionGroups(),
"permissions" => $this->getPermissions(),
"teams" => $this->getTeams()
);
}
}
答案 0 :(得分:0)
尝试这个
class User implements IJsonModel
{
/**
* @ORM\ManyToMany(targetEntity="Team", mappedBy="users", cascade={"persist","remove"})
*/
protected $teams;
public function __construct()
{
$this->teams = new ArrayCollection();
}
/**
* @return ArrayCollection
*/
public function getTeams()
{
return $this->teams;
}
/**
* @param Team $team
*/
public function addTeam(Team $team)
{
if ($this->teams->contains($team))
return;
$this->teams->add($team);
}
}
class Team implements IJsonModel
{
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="teams")
*/
protected $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* @return ArrayCollection
*/
public function getUsers()
{
return $this->users;
}
/**
* @param User $user
*/
public function addUser(User $user)
{
if ($this->users->contains($user))
return;
$this->users->add($user);
$user->addTeam($this);
}
}