我收到了这个错误:
警告:缺少Doctrine \ ORM \ EntityRepository :: __ construct()的参数1
我使用phpstorm进行编码和TestController.php中的行
ExternalProject_Add(
awssdk
PREFIX DIR
GIT_REPOSITORY https://github.com/aws/aws-sdk-cpp.git
CMAKE_ARGS
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DBUILD_ONLY=dynamodb$<SEMICOLON>kinesis
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/Ext/Aws/CtcInstall
-DENABLE_TESTING=OFF
)
带有消息的下划线标有:
必需参数$ em缺少。
调用参数类型与声明的不兼容。
但我还没有使用new ProductRepository();
参数。
我使用3个文件:
$em
AppBundle
|__Controller
| |__ TestController.php
|__Entity
|_______ Product.php
|_______ ProductRepository.php
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\EntityRepository;
use AppBundle\Entity\ProductRepository;
use Symfony\Component\HttpFoundation\Response;
class TestController extends Controller
{
/**
* @Route("/test", name="test")
*/
public function indexAction()
{
$pr = new ProductRepository();
return new Response('OK '.$pr->test());
}
}
<?php
// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="product")
* @ORM\Entity(repositoryClass="AppBundle\Entity\ProductRepository")
*/
class Product
{ /* ......CODE ...*/}
答案 0 :(得分:1)
通过doctrine服务获取存储库,控制器通过getDoctrine
方法返回doctrine服务
public function indexAction()
{
$pr = $this->getDoctrine()->getRepository('AppBundle:Product');
return new Response('OK '.$pr->test());
}
答案 1 :(得分:-2)
您不应直接创建存储库。为此目的使用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());
}
}