Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用symfony2从数据库恢复数据_Symfony_Doctrine - Fatal编程技术网

如何使用symfony2从数据库恢复数据

如何使用symfony2从数据库恢复数据,symfony,doctrine,Symfony,Doctrine,我需要显示数据库中的产品信息,但我有一个 错误:控制器 “UserBundle\Controller\DefaultController::profileAction()”需要 为“$id”参数提供一个值(因为没有 默认值或,因为在此之后有一个非可选参数 一) 以下是profileAction: public function profileAction($id) { $product = $this->getDoctrine() ->getRep

我需要显示数据库中的产品信息,但我有一个

错误:控制器 “UserBundle\Controller\DefaultController::profileAction()”需要 为“$id”参数提供一个值(因为没有 默认值或,因为在此之后有一个非可选参数 一)

以下是profileAction:

public function profileAction($id)
    {
      $product = $this->getDoctrine()
        ->getRepository('UserBundle:Product')
        ->find($id);
        if (!$product) {
        throw $this->createNotFoundException(
            'No product found for id '.$id
    );
        }
      return $this->render('UserBundle:Default:profile.html.twig', array('product' =>$product));
}

请有人帮帮我。

您需要提供$id作为参数

将返回更改为类似以下内容:

return $this->render('UserBundle:Default:profile.html.twig', ['product' => $product, 'id' => $id]);

你需要这样的路线:

_my_route:
    path: /my_path/{id}
    defaults: {controller: AcmeBundle:Default:profile}
    requirements:
        id: \d+
控制器中的操作:

public function profileAction($id)
{
  $product = $this->getDoctrine()
    ->getRepository('UserBundle:Product')
    ->find($id);
    if (!$product) {
    throw $this->createNotFoundException(
        'No product found for id '.$id
);
    }
  return $this->render('UserBundle:Default:profile.html.twig', array('product' =>$product));
以及您视图中的链接或其他类似内容(希望您使用Twig):

控制器:

public function profileAction(Request $request){

    $form = $this->createForm(new SearchType());
    $products = null ;
    if($request->isMethod('POST')){
        $form->handleRequest($request);

        $value = ($form['search']->getData());// change de 'search' according to your form
        $em = $this->getDoctrine()->getManager() ;
        $product = $em->getRepository('UserBundle:Product')->find($value) ;
    }
     if(!$product) throw $this->createNotFoundException('your message');

    return $this->render('UserBundle:Default:profile.html.twig',array(
        'product' => $product
    ));
}
最后,视图中的表单:

<form method="POST" action="{{ path('_profile') }}">
//your code
</form> 

//你的代码

我认为这里$id变为null,这会导致错误,因为$id用于从数据库中获取数据,所以为$id check设置默认值$id将永远不会变为null或空。祝您一切顺利。

错误为您提供了所需的所有信息。“要求您为“$id”参数“您的路由/url中需要类似于?id=5的值。这是我的路由:user\u profile:path:/profile/{id}默认值:{u controller:UserBundle:Default:profile}”方法:get但是现在我被迫在url中输入我正在获取的产品的id,如果没有,我会得到这个错误:找不到“get/profile”(from)的路由。您还可以更改模板中的{{path}}部分,并生成路径
更改{{path}是什么意思模板中的节???我的路径是:href=“{path('user_profile',{'id':product.id}}”)我还可以更改什么???产品是否有id?这可能是您的问题。删除链接并使用
{{dump(product.id)}检查
。可能您的某些产品没有id。因此将使用参数“”id=NULL生成配置文件显示路由。控制器中的操作:公共函数配置文件操作(请求$Request){$form=$this->createForm(new SearchType());$product=NULL;if($Request->isMethod('Post'){$form->handleRequest($Request);$product=($form['id']->getData());$em=$this->getDoctrine()->getManager();$product=$em->getRepository('UserBundle:product')->find($id);}如果(!$product)抛出$this->createNotFoundException('ce produit n existe pas'.$id);返回$this->render('UserBundle:Default:profile.html.twig',',数组('product'=>$product));}更改后,我有一个错误:在控制台中找不到“GET/profile”(来自“@meriem man:Do”php应用程序/控制台调试:路由器)的路由:找到路由了吗?但当我按下按钮时:“rechercher”url中显示的路由是/profile,没有我在输入中输入的id。在视图中,我调用了路由:href=“{path('user_profile',{id':product.id}}”)尝试一个不带参数的POST方法(在路由中等);在控制器中,您可以执行$form->handleRequest($request);$value=($form['search']->getData())并执行查询。您明白我的意思吗?
public function profileAction(Request $request){

    $form = $this->createForm(new SearchType());
    $products = null ;
    if($request->isMethod('POST')){
        $form->handleRequest($request);

        $value = ($form['search']->getData());// change de 'search' according to your form
        $em = $this->getDoctrine()->getManager() ;
        $product = $em->getRepository('UserBundle:Product')->find($value) ;
    }
     if(!$product) throw $this->createNotFoundException('your message');

    return $this->render('UserBundle:Default:profile.html.twig',array(
        'product' => $product
    ));
}
<form method="POST" action="{{ path('_profile') }}">
//your code
</form>