Symfony2:在命令中使用存储库

时间:2021-12-17 07:35:06

I'm using Symfony2 (2.6) and I want to use a custom repository of a Contact entity in a command whose name is CRM:fetchEmails.

我正在使用Symfony2(2.6),我想在名为CRM的命令中使用Contact实体的自定义存储库:fetchEmails。

I tried two different methods in order to get the repository:

我尝试了两种不同的方法来获取存储库:

First method:

In the execute function of my command file

在我的命令文件的执行功能中

$repository = $this->getContainer()->get('doctrine')->getEntityManager()->getRepository('CRMBundle:Contact');

Second method:

In the execute function of my command file

在我的命令文件的执行功能中

$repository = $this->getContainer()->get('myrepository');

And in config.yml

在config.yml中

myrepository:
    class: Doctrine\ORM\EntityRepository
    factory: [@doctrine.orm.entity_manager, getRepository]
    arguments:
        - CRMBundle\Entity\Contact

Either with the first or second method I get this error when executing the command:

使用第一种或第二种方法时,我在执行命令时遇到此错误:

 [Symfony\Component\Debug\Exception\ContextErrorException] Notice: Undefined property: Doctrine\ORM\Configuration::$getRepository CRM:fetchEmails [name] 

Does anyone know how to solve my problem?

有谁知道如何解决我的问题?

2 个解决方案

#1


1  

I would inherit ContainerAwareCommand to solve this problem :

我会继承ContainerAwareCommand来解决这个问题:

public class MyCommand extends ContainerAwareCommand
{ ... }

ContainerAwareCommand is a base class for Command, but has access to the container (just like you would do in a Controller-based class, so you can access your repo by doing $this->getContainer()->getDoctrine()->getRepository("BundleName:MyRepo").

ContainerAwareCommand是Command的基类,但是可以访问容器(就像在基于Controller的类中一样,所以你可以通过执行$ this-> getContainer() - > getDoctrine() - > getRepository来访问你的repo。 ( “BUNDLENAME:MyRepo”)。

Let me know if it helps !

如果有帮助请告诉我!

#2


0  

I usually use this, in the services.yml:

我通常在services.yml中使用它:

myrepository:
        class: Doctrine\ORM\EntityRepository
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments: [ 'CRMBundle\Entity\Contact' ]

Hope this help

希望这有帮助

#1


1  

I would inherit ContainerAwareCommand to solve this problem :

我会继承ContainerAwareCommand来解决这个问题:

public class MyCommand extends ContainerAwareCommand
{ ... }

ContainerAwareCommand is a base class for Command, but has access to the container (just like you would do in a Controller-based class, so you can access your repo by doing $this->getContainer()->getDoctrine()->getRepository("BundleName:MyRepo").

ContainerAwareCommand是Command的基类,但是可以访问容器(就像在基于Controller的类中一样,所以你可以通过执行$ this-> getContainer() - > getDoctrine() - > getRepository来访问你的repo。 ( “BUNDLENAME:MyRepo”)。

Let me know if it helps !

如果有帮助请告诉我!

#2


0  

I usually use this, in the services.yml:

我通常在services.yml中使用它:

myrepository:
        class: Doctrine\ORM\EntityRepository
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments: [ 'CRMBundle\Entity\Contact' ]

Hope this help

希望这有帮助