Php Symfony 3.1/条令、存储库或包含路径错误
我得到了这个错误: 警告:缺少条令\ORM\EntityRepository::\uu构造()的参数1 我使用phpstorm进行编码,并在TestController.php中使用该行Php Symfony 3.1/条令、存储库或包含路径错误,php,symfony,doctrine,Php,Symfony,Doctrine,我得到了这个错误: 警告:缺少条令\ORM\EntityRepository::\uu构造()的参数1 我使用phpstorm进行编码,并在TestController.php中使用该行 new ProductRepository(); 下面是一条信息: 所需参数$em缺失较少 调用参数类型与声明的不兼容 但是我还没有使用$em参数 我使用3个文件: AppBundle |__Controller | |__ TestController.php
new ProductRepository();
下面是一条信息:
所需参数$em缺失较少
调用参数类型与声明的不兼容
但是我还没有使用$em
参数
我使用3个文件:
AppBundle
|__Controller
| |__ TestController.php
|__Entity
|_______ Product.php
|_______ ProductRepository.php
TestController.php:
您不应该直接创建存储库。为此,请使用
EntityManager
。您可以尝试以下代码:
class TestController extends Controller
{
private $entityManager;
public function __construct(\Doctrine\ORM\EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/test", name="test")
*/
public function indexAction()
{
$pr = $this->entityManager->getRepository('AppBundle\Entity\ProductRepository');
return new Response('OK '.$pr->test());
}
}
通过条令服务获取存储库,控制器通过
getdoctor
方法返回条令服务
public function indexAction()
{
$pr = $this->getDoctrine()->getRepository('AppBundle:Product');
return new Response('OK '.$pr->test());
}
控制器由symfony自动启动,并具有获取任何服务的方法,包括条令和实体manager@MaxP. 我不知道。谢谢你的提示。在本例中,我想使用TestController.php中ProductRepository中的方法test,
namespace AppBundle\Entity;
use Doctrine\ORM\EntityRepository;
class ProductRepository extends EntityRepository
{
public function test()
{
return 'hello';
}
}
class TestController extends Controller
{
private $entityManager;
public function __construct(\Doctrine\ORM\EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/test", name="test")
*/
public function indexAction()
{
$pr = $this->entityManager->getRepository('AppBundle\Entity\ProductRepository');
return new Response('OK '.$pr->test());
}
}
public function indexAction()
{
$pr = $this->getDoctrine()->getRepository('AppBundle:Product');
return new Response('OK '.$pr->test());
}