我正在尝试使用symfony / doctrine创建一个小应用程序来读取表中的记录。 我的步骤:
3)创建了GS \ OrderBundle \ Entity \ CustomerRepository.php(我不知道为什么我还需要它,它是自动生成的)。源代码:
namespace GS\OrderBundle\Entity;
use Doctrine\ORM\EntityRepository;
class CustomerRepository extends EntityRepository
{
}
4)修改了CustomerController.php:
<?php
namespace GS\OrderBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use GS\OrderBundle\Entity\Customer;
use Doctrine\ORM\EntityManager;
class CustomerController
{
public function indexAction($id)
{
$customer = $this->getDoctrine()->getRepository('GSOrderBundle:Customer')->find($id);
return new Response(
'<html><body>Number: '.$id.'</body></html>'
);
}
}
收到以下错误:
尝试调用方法&#34; getDoctrine&#34;在课堂上 &#34; GS \ OrderBundle \控制器\ CustomerController&#34;
500内部服务器错误 - UndefinedMethodException
堆栈跟踪
在第13行的src / GS / OrderBundle / Controller / CustomerController.php中 - 公共函数indexAction($ id) { $ customer = $ this-&gt; getDoctrine() - &gt; getRepository(&#39; GSOrderBundle:Customer&#39;) - &gt; find($ id); 返回新的回应( &#39;号码:&#39;。$ id。&#39;&#39; );日志 -
1错误信息 - 匹配的路线&#34;客户&#34; (参数:&#34; _controller&#34;: &#34; GS \ OrderBundle \ Controller \ CustomerController :: indexAction&#34;,&#34; id&#34;: &#34; 1784&#34;,&#34; _route&#34;:&#34; customer&#34;)DEBUG - 通知事件&#34; kernel.request&#34; 听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ DebugHandlersListener ::配置&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ ProfilerListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ DumpListener ::配置&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\捆绑\ FrameworkBundle \事件监听\ SessionListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ FragmentListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ RouterListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ LocaleListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ TranslatorListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\安全\ HTTP \防火墙:: onKernelRequest&#34 ;.调试 - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\捆绑\ AsseticBundle \事件监听\ RequestListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Symfony的\捆绑\ FrameworkBundle \ DataCollector \ RouterDataCollector :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Acme公司\ DemoBundle \事件监听\ ControllerListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Symfony的\元器件\ HttpKernel \ DataCollector \ RequestDataCollector :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ ControllerListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ ParamConverterListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ HttpCacheListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ SecurityListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ TemplateListener :: onKernelController&#34 ;. 关键 - 未捕获的PHP异常 Symfony \ Component \ Debug \ Exception \ UndefinedMethodException:&#34; Attempted 调用方法&#34; getDoctrine&#34;在课堂上 &#34; GS \ OrderBundle \控制器\ CustomerController&#34;&#34;在 C:\ XAMPP \ htdocs中\ goodstuff \ SRC \ GS \ OrderBundle \控制器\ CustomerController.php 第13行DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ DebugHandlersListener ::配置&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ ProfilerListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ DumpListener ::配置&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\捆绑\ FrameworkBundle \事件监听\ SessionListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ FragmentListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ RouterListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ LocaleListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\ HttpKernel \事件监听\ TranslatorListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\元器件\安全\ HTTP \防火墙:: onKernelRequest&#34 ;.调试 - 通知事件&#34; kernel.request&#34;听众 &#34; Symfony的\捆绑\ AsseticBundle \事件监听\ RequestListener :: onKernelRequest&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Symfony的\捆绑\ FrameworkBundle \ DataCollector \ RouterDataCollector :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Acme公司\ DemoBundle \事件监听\ ControllerListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Symfony的\元器件\ HttpKernel \ DataCollector \ RequestDataCollector :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ ControllerListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ ParamConverterListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ HttpCacheListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ SecurityListener :: onKernelController&#34 ;. DEBUG - 通知事件&#34; kernel.controller&#34;听众 &#34; Sensio公司\捆绑\ FrameworkExtraBundle \事件监听\ TemplateListener :: onKernelController&#34;
答案 0 :(得分:6)
您需要扩展Symfony的Controller
类才能使用getDoctrine()
方法。
所以:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class CustomerController extends Controller
{
public function indexAction($id)
{
$customer = $this->getDoctrine()->getRepository('GSOrderBundle:Customer')->find($id);
return new Response(
'<html><body>Number: '.$id.'</body></html>'
);
}
}
(你也可以define controller as a service,但这是更复杂的解决方案)