【问题标题】:Symfony override third party entity repositorySymfony 覆盖第三方实体存储库
【发布时间】:2017-11-15 17:14:16
【问题描述】:

我正在使用带有自定义原则实体存储库的第三方捆绑包。它包含一个我想重写的功能,以根据我的需要对其进行自定义。 有没有人这样做过?我找不到任何有关覆盖实体存储库的文档。 为什么我需要这样做:来自该第三方包的服务调用此存储库函数并执行它。但我想改变其中的一部分。

我尝试了什么: 创建一个自己的实体存储库类:

namespace NotificationBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Citrax\Bundle\DatabaseSwiftMailerBundle\Entity\Email;
use Citrax\Bundle\DatabaseSwiftMailerBundle\Entity\EmailRepository as BaseRepository;

class EmailRepository extends BaseRepository
{

    public function markCompleteSending(Email $email)
    {
        $email->setStatus(Email::STATUS_COMPLETE);
        $email->setSentAt(new \DateTime());
        $email->setErrorMessage('');
        $em = $this->getEntityManager();
        $em->remove($email);
        $em->flush();
    }
}

它扩展了父存储库并只覆盖了这个函数,其余的我没有触及。然后我将该存储库注册为服务。 无论如何它不起作用! 还需要什么吗?请告诉我!我很高兴能得到一些帮助。

调用函数的位置:

public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
{
    if (!$transport->isStarted())
    {
        $transport->start();
    }

    $count = 0;
    $emails = $this->repository->getEmailQueue($this->getMessageLimit());

    foreach($emails as $email){
        /*@var $message \Swift_Mime_Message */
        $message = $email->getMessage();
        try{
            $count_= $transport->send($message, $failedRecipients);
            if($count_ > 0){
                $this->repository->markCompleteSending($email);
                $count += $count_;
            }else{
                throw new \Swift_SwiftException('The email was not sent.');
            }
        }catch(\Swift_SwiftException $ex){
            $this->repository->markFailedSending($email, $ex);
        }
    }

    return $count;
}

这就是类 DatabaseSpool 注册为我的 swiftmailer 假脱机的服务。

【问题讨论】:

  • 我猜你需要在供应商的 git repo 上为这个功能打开一个问题
  • @MKhalidJunaid 好的。我认为这是一个更普遍的事情?
  • 代码在哪里调用?
  • @Tokeeen.com 我添加了调用代码的函数! :)

标签: symfony doctrine repository overriding


【解决方案1】:

要覆盖DatabaseSwiftMailerBundle 的repo,我可以看到(on git) 供应商已将Citrax\Bundle\DatabaseSwiftMailerBundle\Entity\EmailRepository 定义为名为@9​​87654326@ 的服务,您可以使用symfony 的CompilerPassInterface 覆盖服务。在 DependencyInjection 文件夹下的包中,创建编译器传递并通过定义应扩展供应商的 repo 的 repo 覆盖 repository.email 服务。

namespace Your\Bundle\DependencyInjection\Compiler;

use NotificationBundle\Entity\EmailRepository;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class OverrideServiceCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container->getDefinition('repository.email');
        $definition->setClass(EmailRepository::class);
    }
}

根据官方文档

如果您想修改另一个包的服务定义,您可以使用编译器传递来更改服务的类或修改方法调用。在以下示例中,original-service-id 的实现类更改为 Acme\DemoBundle\YourService。

更多信息请查看Services & Configuration

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2019-07-12
    • 2014-07-04
    • 2014-03-07
    • 2017-05-10
    相关资源
    最近更新 更多