在 Symfony2 中设置关系之间的方法
Set methods between relationships in Symfony2
我有 2 个实体 - 用户 和 项目。他们之间的关系是这样的:
// Acme/MyBundle/Entity/Project.php
...
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="projects")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
*/
private $author;
public function setAuthor(\Acme\MyBundle\Entity\User $author = null)
{
$this->author = $author;
return $this;
}
... other set/get methods...
和
// Acme/MyBundle/Entity/User.php
...
/**
* @ORM\OneToMany(targetEntity="Project", mappedBy="author")
*/
private $projects;
public function addProject(\Acme\MyBundle\Entity\Project $projects)
{
$this->projects[] = $projects;
return $this;
}
... other set/get methods...
当我尝试创建项目并将当前用户指定为作者(并将项目添加到用户的字段中)时出现问题。
这是我在项目控制器中的 createAction:
public function createAction(Request $request, $user_id)
{
$entity = new Project();
// THE PROBLEM PART
$entity->setAuthor($user_id);
$user = getUser($user_id); // get the user and attach the project
$user->addProject($entity->getId());
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect('homepage');
}
return $this->render('AcmeMyBundle:Project:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
显然它 returns 我的错误是 "Argument 1 passed to ... must be instance of ..."。
有什么解决办法吗?
p.s。这是我第一次尝试学习 symfony2
在这种情况下,你可以只给 Doctrine(Symfony 的默认 ORM)对象本身而不是它的 id。 Doctrine 会发现它只需要将 id 保存到数据库中。
所以会是:
$user = $this->getUser($user_id);
$entity->setAuthor($user);
你也不需要在项目上设置它,这个也由 Doctrine 处理。
我有 2 个实体 - 用户 和 项目。他们之间的关系是这样的:
// Acme/MyBundle/Entity/Project.php
...
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="projects")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
*/
private $author;
public function setAuthor(\Acme\MyBundle\Entity\User $author = null)
{
$this->author = $author;
return $this;
}
... other set/get methods...
和
// Acme/MyBundle/Entity/User.php
...
/**
* @ORM\OneToMany(targetEntity="Project", mappedBy="author")
*/
private $projects;
public function addProject(\Acme\MyBundle\Entity\Project $projects)
{
$this->projects[] = $projects;
return $this;
}
... other set/get methods...
当我尝试创建项目并将当前用户指定为作者(并将项目添加到用户的字段中)时出现问题。
这是我在项目控制器中的 createAction:
public function createAction(Request $request, $user_id)
{
$entity = new Project();
// THE PROBLEM PART
$entity->setAuthor($user_id);
$user = getUser($user_id); // get the user and attach the project
$user->addProject($entity->getId());
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect('homepage');
}
return $this->render('AcmeMyBundle:Project:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
显然它 returns 我的错误是 "Argument 1 passed to ... must be instance of ..."。
有什么解决办法吗?
p.s。这是我第一次尝试学习 symfony2
在这种情况下,你可以只给 Doctrine(Symfony 的默认 ORM)对象本身而不是它的 id。 Doctrine 会发现它只需要将 id 保存到数据库中。
所以会是:
$user = $this->getUser($user_id);
$entity->setAuthor($user);
你也不需要在项目上设置它,这个也由 Doctrine 处理。