PHP-get关系
我有一个用户模型,与Profil有一对多(单向连接表)关系 但当我这么做的时候PHP-get关系,php,doctrine-orm,Php,Doctrine Orm,我有一个用户模型,与Profil有一对多(单向连接表)关系 但当我这么做的时候 $user = $userRepository = $entityManager->getRepository('User'); $user = $userRepository->findOneBy( array('username' => $username, 'password' => $password)
$user = $userRepository = $entityManager->getRepository('User');
$user = $userRepository->findOneBy(
array('username' => $username,
'password' => $password)
);
echo $user->getProfils()->getLibelle();
或
我什么都没有。没有错误。没有结果。
我的表已创建并完成
如果我这样做
$user = $userRepository = $entityManager->getRepository('User');
$user = $userRepository->findOneBy(
array('username' => $username,
'password' => $password)
);
echo $user->getUsername();
我有很好的成绩
User.php模型
<?php
/**
* @Entity @Table(name="users")
**/
class User
{
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $username;
/** @Column(type="string") **/
protected $password;
/** @Column(type="string") **/
protected $email;
/**
* Many User have Many Profils.
* @ManyToMany(targetEntity="Profil")
* @JoinTable(name="users_profils",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="profil_id", referencedColumnName="id", unique=false)}
* )
*/
private $profils;
public function __construct()
{
$this->profils = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getUsername()
{
return $this->username;
}
public function setUserName($username)
{
$this->username = $username;
}
public function getPassword()
{
return $this->password;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getProfils()
{
return $this->profils;
}
}
?>
User.php:
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $username;
/** @Column(type="string") **/
protected $password;
/** @Column(type="string") **/
protected $email;
/**
* @OneToMany(targetEntity="Profil", mappedBy="user")
*/
private $profils;
我试过这个:
print_r($user->getProfils()->toArray());
结果:Array()好的,我找到了解决方案,如果这对某人有帮助: 这是最后的代码 User.php
class User
{
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $username;
/** @Column(type="string") **/
protected $password;
/** @Column(type="string") **/
protected $email;
/**
* Many User have Many Profils.
* @ManyToMany(targetEntity="Profil")
* @JoinTable(name="users_profils",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="profil_id", referencedColumnName="id", unique=false)}
* )
*/
protected $profils;
public function __construct()
{
$this->profils = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getProfils()
{
return $this->profils ;
}
Profil.php
/**
* @Entity @Table(name="profils")
**/
class Profil
{
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $libelle;
/** @Column(type="integer") **/
protected $level;
protected $user;
现在,我们可以从一个用户获得所有Profil
$userRepository = $entityManager->getRepository('User');
$user = $userRepository->findOneBy(
array('username' => $username,
'password' => $password)
);
$profils = $user->getProfils()->toArray();
foreach($profils as $profil){
echo $profil->getLibelle() .'<br>';
echo $profil->getLevel().'<br>';
echo $profil->getId().'<br>';
}
我有很多亲戚。
因此,用户可以拥有许多Profil。首先,您的用户实体中有许多Profil。在你的个人资料中没有任何关系。Symfony文档还有一些例子可能会有所帮助:去掉@ORM前缀。好的,你能看看新代码吗?thxSo问题是什么,解决方案是什么。你没有在你的答案中解释这些,你所做的只是在没有任何注释的情况下发布大量代码。这对任何人都有什么帮助?谢谢你的评论,我更新了解决方案。我希望它更清楚。
class User
{
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $username;
/** @Column(type="string") **/
protected $password;
/** @Column(type="string") **/
protected $email;
/**
* Many User have Many Profils.
* @ManyToMany(targetEntity="Profil")
* @JoinTable(name="users_profils",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="profil_id", referencedColumnName="id", unique=false)}
* )
*/
protected $profils;
public function __construct()
{
$this->profils = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getProfils()
{
return $this->profils ;
}
/**
* @Entity @Table(name="profils")
**/
class Profil
{
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $libelle;
/** @Column(type="integer") **/
protected $level;
protected $user;
$userRepository = $entityManager->getRepository('User');
$user = $userRepository->findOneBy(
array('username' => $username,
'password' => $password)
);
$profils = $user->getProfils()->toArray();
foreach($profils as $profil){
echo $profil->getLibelle() .'<br>';
echo $profil->getLevel().'<br>';
echo $profil->getId().'<br>';
}
$profils = $user->getProfils()->toArray();
for($profils as profil){
$libelle = $profil->getLibelle()
}