我在一个带有Doctrine 2.0的laravel 5.4中有一个简单的项目。我有三个表:用户,角色, user_roles 。来自HEIDISQL的表模式的屏幕截图,如下所示:
我当然为每个表都有三个实体类:
<?php
namespace TodoList\Http\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
*/
private $id;
/**
*
* @ORM\Column(type="string")
*
*/
private $name;
/**
*
* @ORM\Column(type="string")
*
*/
private $email;
/**
* @ORM\OneToMany(targetEntity="Task", mappedBy="user", cascade={"persist"})
* @var ArrayCollection|Task[]
*/
private $tasks;
/**
* @ORM\OneToMany(targetEntity="UserRole", mappedBy="user")
*/
protected $user_roles;
/**
* User constructor
* @param @name
* @param @email
* @param $password
*/
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
$this->tasks = new ArrayCollection();
$this->user_roles = new ArrayCollection();
}
/**
*
* @return mixed
*
*
*/
public function getName()
{
return $this->name;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @return mixed
*/
public function getTasks()
{
return $this->tasks;
}
public function addTask(Task $task)
{
if(!$this->tasks->contains($task)) {
$task->setUser($this);
$this->tasks->add($task);
}
}
public function getId(){
return $this->id;
}
public function getUserRoles(){
return $this->user_roles;
}
}
<?php
namespace TodoList\Http\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="roles")
*
*/
class Role{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string")
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @ORM\OneToMany(targetEntity="UserRole", mappedBy="role",cascade={"persist"})
*/
protected $user_roles;
public function getId(){
return $this->id;
}
public function getName(){
return $this->name;
}
public function getDescription(){
return $this->description;
}
}
<?php
namespace TodoList\Http\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="user_roles")
*/
class UserRole
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="user_roles")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*
*/
private $user;
/**
*
* @ORM\ManyToOne(targetEntity="Role", inversedBy="user_roles")
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
*/
private $role;
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \NVC\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set recipe
*
* @param \NVC\RecipeBundle\Entity\Recipe $recipe
* @return UserRecipeAssociation
*/
public function setRole(Role $role)
{
$this->role = $role;
return $this;
}
/**
* Get recipe
*
* @return \NVC\RecipeBundle\Entity\Recipe
*/
public function getRole()
{
return $this->role;
}
}
当我尝试通过doctrine Entity Managar检索特定用户的角色时,我遇到了问题。我试图以这种方式做到这一点:
public function showUserRoles(EntityManagerInterface $em){
$userRoles = $em->getRepository('\TodoList\Http\Entities\UserRole');
$userRoles->findBy(['user' => 2]);
//count($userRole);
}
方法 findBy 出错 错误信息是:
(1/1)FatalErrorException
学说\ COMMON \代理\ AbstractProxyFactory :: getProxyDefinition(): 打开失败 'C:\ XAMPP \ htdocs中\项目\存储\ proxies__CG__TodoListHttpEntitiesRole.php' (include_path中= 'C:\ XAMPP \ PHP \ PEAR')
我的实体有什么问题吗?我不知道怎么能解决这个问题。有人可以帮助我吗? 我会非常致命的 最好的问候;)
答案 0 :(得分:0)
您需要将文件夹C:\xampp\htdocs\Project\storage\proxies\
设置为可写,然后您需要通过console命令生成代理:
doctrine:generate:proxies