У меня есть модель пользователя с One-ко-многим (однонаправленный с Join Table) отношения с ProfilPHP - Доктрина получить отношения
Но когда я
$user = $userRepository = $entityManager->getRepository('User');
$user = $userRepository->findOneBy(
array('username' => $username,
'password' => $password)
);
echo $user->getProfils()->getLibelle();
или
echo $user->getProfils()[0];
У меня ничего нет. Нет ошибок. Нет результатов. Мои таблицы создаются и завершаются.
Если я
$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;
}
}
?>
Profil.php Модель
<?php
/**
* @Entity @Table(name="profils")
**/
class Profil
{
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $libelle;
/** @Column(type="integer") **/
protected $level;
public function getId()
{
return $this->id;
}
public function getLibelle()
{
return $this->libelle;
}
public function setLibelle($libelle)
{
$this->libelle = $libelle;
}
public function getLevel()
{
return $this->level;
}
public function setLevel($level)
{
$this->level = $level;
}
}
?>
Не могли бы вы сказать мне, что я не хватает?
Заранее спасибо.
UPDATE: Profil.php:
class Profil
{
/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;
/** @Column(type="string") **/
protected $libelle;
/** @Column(type="integer") **/
protected $level;
/**
* @ManyToOne(targetEntity="User", inversedBy="profils")
* @JoinColumn(unique=false)
*/
private $user;
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()
Для начала у вас есть ManyToMany в вашей пользовательской сущности. И вообще никакого отношения к вашему профилю.В документах Symfony есть еще несколько примеров, которые могут помочь: http://symfony.com/doc/current/doctrine.html Просто отпустите префикс @ORM. – Cerad
Хорошо, вы можете взглянуть на новый код PLS? thx – bakouz