Php 无法猜测如何获取条令实例

Php 无法猜测如何获取条令实例,php,doctrine-orm,doctrine,symfony,Php,Doctrine Orm,Doctrine,Symfony,我已经完成了从Symfony 2.8到Symfony 3的项目更新过程,现在正在对我的一个控制器进行返工,但遇到了一些问题 我有一个子实体和控制器。控制器有一个基本路由 /** * Child controller. * * @Route("/profile/{parentName}/{childName}") */ 用行动 /** * Finds and displays a Child entity. * * @Route("/{id}", name="child_show") *

我已经完成了从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()
*/
/**
* 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(),
    );
}
我遇到一个错误,无法猜测如何从请求信息中获取条令实例。我猜是因为需要Id才能获得正确的Child实例?但是,子实体中的childName($name)也是唯一的

我有点受不了了。谁能告诉我哪里做错了?我希望能够为不包含ID但使用孩子的名字返回所需信息的孩子配置文件页面创建路由

更新对一些评论/问题的回应-以下是我的子实体

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修改为

/**
* @Route("/", name="child_show")
* @Method("GET")
* @Template()
*/
/**
* 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对象未找到


如果有唯一字段,则应在实体定义中将其标记为唯一字段:

在:

或:

如果为
name
添加
@列
唯一约束,则您的
findOneByName
可能会按预期工作

现在,您还可以使其工作如下:

$childs = $em->getRepository('AppBundle:Child')->findBy( array('name' => $childName ));
然后你得到一个
集合
,如果你确定它是唯一的,你可以简单地得到元素,如果集合不是


找不到实体,因为无法找到名为
{name}
和parentName
parentName
的子项。。。这似乎是合法的,因为子实体中不存在parentName

两种解决方案:

1-从未尝试过,但可能会成功:在Child中创建getParentName(),并使其
返回$this->parent->getName()

2-在控制器操作顶部添加ParamConverter注释:

/**
 * Finds and displays a Child entity.
 *
 * @Route("/", name="child_show")
 * @Method("GET")
 * @Template()
 * @ParamConverter("child", class="AppBundle:Child", options={"name" = "name"})
 */
 public function showAction(Child $child)

这样,当试图检索您的子实体时,转换器只考虑参数“名称”…(顺便问一下,这真的是你想要的吗?)


不要忘记在控制器中使用ParamConverter。

我收到了相同的错误消息,但我想,情况与您的稍有不同

我需要在自己的细枝模板中呈现特定的文章。所以我有:

,其中
'title'
是表中列的名称

但在我的控制器课程中,我有:

/**
 * @Route("/article/{name}", name="article_show")
 * @Method("GET")
 */
public function showAction(Article $article)
{
    return $this->render('AppBundle:Article:show.html.twig', ['article'=>$article]);
}
@Route
中的
{name}
改变了一切。我的意思是这是造成错误的原因。因此,我需要确保我的锚元素(
{title':article.title | lower}
)的
{title}
部分应该有
{title}


我收到了同样的错误信息,但我想,与你的有点不同

无法为命名路由“users\u delete”生成URL,因为该路由不存在

我忘了在routing.yml的路由中添加请求的{variable}

users_delete:
    path:     /{id}/modifier
    defaults: { _controller: ReservationBundle:Users:modifier }
这就是为什么我的控制器无法访问$request变量中预期的信息:

公共函数修改(请求$Request,用户$user) {}

但你的回答让我走上了正确的道路:

@Route中的{name}改变了一切。我的意思是那是导致 错误。所以我需要确定,我应该 将{title}作为我的锚元素({'title')的'title'部分: 第条.标题|下})


parentName
childName
是否是用于匹配和获取实体的属性的确切名称?如果没有,您应该使用ParamConverter注释。@Sogara不,它们不是。我稍后再查看,谢谢!在子实体上定义的父实体,如*@ORM\manytone(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中,子ID就会被返回。@Sogara我已经更新了我的问题以使用相同的属性,但是现在我得到了一个AppBundle\Entity\child object not found错误。谢谢@Wilt-今天稍后我会仔细看一看。我的子实体@ORM\Column(name=“name”,type=“string”)上的名称是唯一的,length=255,但需要查看您的一些其他建议我已经发布了我的子实体,并且似乎具有您所描述的正确注释,但是我得到了一个AppBundle\Entity\Child object not found错误。您有什么想法吗?@StuartBrown它在正确的文件夹中吗?确保删除了类顶部之间的所有空白还有注释…谢谢-非常好用!实际上我想在检索配置文件时考虑parentName和childName(现在的名称),但我想我可以使用其他参数转换器来排序。要了解的全新功能:)。我会将此标记为答案。再次感谢。谢谢!:)我不完全确定您是否可以使用ParamConverter来考虑关系中的字段,但请尝试一下,否则您可以在控制器中手动执行此操作。祝你好运
/**
 * @Route("/article/{name}", name="article_show")
 * @Method("GET")
 */
public function showAction(Article $article)
{
    return $this->render('AppBundle:Article:show.html.twig', ['article'=>$article]);
}
users_delete:
    path:     /{id}/modifier
    defaults: { _controller: ReservationBundle:Users:modifier }