Symfony 生成器生成了以下存储库类:
namespace AppBundle\Repository;
use AppBundle\Entity\GroupEntity;
/**
* GroupEntityRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class GroupEntityRepository extends \Doctrine\ORM\EntityRepository
{
}
服务.yml:
group_entity_repository:
class: AppBundle\Repository\GroupEntityRepository
arguments: ["@doctrine.orm.entity_manager", AppBundle\Entity\GroupEntity]
我错误地配置了 services.yml,但我现在不知道使用什么作为第二个参数。所以我收到错误:
Catchable Fatal Error: Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, string given, called in E:\other\dropbox\Dropbox\programavimas\kodo pavyzdziai\htdocs\users_admin_demo\var\cache\dev\appDevDebugProjectContainer.php on line 1626 and defined
如何解决?我在文档中看不到,它只显示了生成器和最终生成的类的代码,但没有服务配置。
最佳答案
从 Symfony 3.3 开始推荐:
从 Symfony 3.3 开始,建议使用实际的类名作为服务 ID( read this 和 this )。
AppBundle\Repository\GroupEntityRepository:
factory: 'Doctrine\ORM\EntityManagerInterface:getRepository'
arguments:
- AppBundle\Entity\GroupEntity
原始答案:
您可以像这样配置存储库服务:
group_entity_repository:
class: AppBundle\Repository\GroupEntityRepository
factory: ["@doctrine.orm.entity_manager", getRepository]
arguments:
- AppBundle\Entity\GroupEntity
您可能永远不想自己调用存储库构造函数。因此,这种方法仅使用entity_manager来获取存储库。 服务容器基本上使用此代码来获取存储库:
$container->get('doctrine.orm.entity_manager')->getRepository('AppBundle\Entity\GroupEntity');
关于symfony - 如何在 symfony 3 中为存储库类配置依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38505002/