【发布时间】:2016-05-03 09:19:18
【问题描述】:
我已经完成了将我的项目从 Symfony 2.8 更新到 Symfony 3 的过程,现在我正在重新设计我的一个控制器,但遇到了一些问题。
我有一个子实体和控制器。控制器有一个基本路由
/**
* Child controller.
*
* @Route("/profile/{parentName}/{childName}")
*/
有一个动作
/**
* Finds and displays a Child entity.
*
* @Route("/{id}", name="child_show")
* @Method("GET")
* @Template()
*/
public function showAction(Child $child)
{
$deleteForm = $this->createDeleteForm($child);
return array(
'child' => $child,
'delete_form' => $deleteForm->createView(),
);
}
但是我不希望页面 URL 是 domain.com/parentname/childname/id 我希望它是 domain.com/parentname/childname
在 2.8 中,我的控制器是
/**
* Child controller.
*
* @Route("/profile/{parentName}/{childName}")
*/
/**
* Finds and displays a Child entity.
*
* @Route("/", name="child_show")
* @Method("GET")
* @Template()
*/
public function showAction($childName)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Child')->findOneByName($childName);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Child entity.');
}
$deleteForm = $this->createDeleteForm($childName);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
它按我的意愿工作。
但是,如果在我更新的控制器中,我将 showAction 上的路由注释修改为
/**
* @Route("/", name="child_show")
* @Method("GET")
* @Template()
*/
我收到一个错误无法猜测如何从请求信息中获取 Doctrine 实例。 我猜是因为需要 Id 来获取正确的 Child 实例?但是 Child 实体中的 childName ($name) 也是唯一的。
我对此有点坚持。谁能告诉我我做错了什么?我希望能够为不包含 ID 但使用孩子的姓名返回所需信息的孩子个人资料页面提供路线。
针对一些 cmets / 问题进行更新 - 以下是我的子实体
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\User;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Child
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\ChildRepository")
* @UniqueEntity("name")
*/
class Child
{
/**
* @var integer
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/*
* Todo - figure out how to use a date type for dateofbirth below and as well in users.yml fixtures file
*/
/**
* @var \DateTime
*
* @ORM\Column(name="date_of_birth", type="datetime")
*/
private $dateOfBirth;
//todo find ot why the parent variable here and Id variable in AppBundle:User are not mapped correctly
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="id")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $parent;
/**
* @return User
*/
public function getParent()
{
return $this->parent;
}
/**
* @param User $parent
*/
public function setParent(User $parent)
{
$this->parent = $parent;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Child
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set dateOfBirth
*
* @param \DateTime $dateOfBirth
*
* @return Child
*/
public function setDateOfBirth($dateOfBirth)
{
$this->dateOfBirth = $dateOfBirth;
return $this;
}
/**
* Get dateOfBirth
*
* @return \DateTime
*/
public function getDateOfBirth()
{
return $this->dateOfBirth;
}
}
并将showAction修改为
/**
* Child controller.
*
* @Route("/profile/{parentName}/{name}")
*/
/**
* Finds and displays a Child entity.
*
* @Route("/", name="child_show")
* @Method("GET")
* @Template()
*/
public function showAction(Child $name)
{
$deleteForm = $this->createDeleteForm($name);
return array(
'child' => $name,
'delete_form' => $deleteForm->createView(),
);
}
但我现在得到一个错误 AppBundle\Entity\Child object not found.
ERROR - Uncaught PHP Exception
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "AppBundle\Entity\Child object not found." at /Library/WebServer/Documents/story-project/app/cache/dev/classes.php line 7183
【问题讨论】:
-
parentName和childName是用于匹配和获取实体的属性的确切名称吗?如果没有,您应该使用 ParamConverter 注释。 -
@Sogara 不,他们不是,我稍后再检查一下,谢谢!在子实体上定义的父级 *@ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="id") * @ORM\JoinColumn(onDelete="CASCADE") */ private $parent;但是我认为我在这个版本中也有其他更正,因为目前我可以在 URL 中为 parentName 设置任何值(domain.com/john/sam/56 和 domain.com/mary/sam/56 都可以)等等只要孩子 ID 在 url 中,就会返回孩子。
-
@Sogara 我已经更新了我的问题以使用相同的属性,但我现在得到一个 AppBundle\Entity\Child object not found 错误。
标签: php doctrine-orm doctrine symfony