Shopware:通过其ID获取文件编号

时间:2020-11-10 16:26:34

标签: php symfony shopware

我需要按其ID在documentRepository中获取documentNumber。 我正在执行一个“简单”的DAL请求来执行此操作,但是由于某种原因,转储输出会产生空字段。

这是我的订阅者,其中包含DAL请求:

<?php

declare(strict_types=1);

namespace HmDocumentGeneratorPlugin\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use HmDocumentGeneratorPlugin\Service\HmDocumentService;
use HmDocumentGeneratorPlugin\Service\HmOrderService;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use HmDocumentGeneratorPlugin\Event\InvoiceGeneratedEvent;
use Shopware\Core\Checkout\Order\OrderEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Shopware\Core\Checkout\DocumentEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Emakers\TransmissionPlugin\TransmissionPlugin;
use Emakers\TransmissionPlugin\Services\ExactDataService;

class OrderSubscriber implements EventSubscriberInterface
{

    /**
     * @ContainerInterface $container
     */
    private $container;

    /**
     * @var HmOrderService
     */
    private $service;

    /**
     * @var HmDocumentService
     */
    private $docService;

    /**
     * @var EntityRepositoryInterface
     */
    private $transmissionLogRepository;

    /**
     * @var EntityRepositoryInterface
     */
    private $transmissionRepository;

    /**
     * @var EntityRepositoryInterface
     */
    private $documentRepository;

    /**
     * @var EntityRepositoryInterface
     */
    private $orderRepository;

    public function __construct(ContainerInterface $container, HmOrderService $service, HmDocumentService $docService, EntityRepositoryInterface $transmissionLogRepository, EntityRepositoryInterface $transmissionRepository, EntityRepositoryInterface $documentRepository, EntityRepositoryInterface $orderRepository)
    {
        $this->container = $container;
        $this->service = $service;
        $this->docService = $docService;
        $this->transmissionLogRepository = $transmissionLogRepository;
        $this->transmissionRepository = $transmissionRepository;
        $this->documentRepository = $documentRepository;
        $this->orderRepository = $orderRepository;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
        ];
    }

    public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
    {
        $order = $event->getOrder();

        if ($order instanceof OrderEntity) {
            $this->service->generateDocuments($order); //document correctly generated (I checked in the db)
            $document = $this->docService->getInvoice($order); //To get the documentId
            $documentObject = $this->documentRepository->search(new Criteria([ $document['id'] ]), \Shopware\Core\Framework\Context::createDefaultContext() );
            var_dump($documentObject);
            die('please man');
        }
   }
}

这是我的services.xml:

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

   <services>
       <service id="HmDocumentGeneratorPlugin\Subscriber\OrderSubscriber">
           <argument type="service" id="service_container"/>
           <argument id="HmDocumentGeneratorPlugin\Service\HmOrderService" type="service" />
           <argument id="HmDocumentGeneratorPlugin\Service\HmDocumentService" type="service" />
           <argument type="service" id="order.repository"/>
           <argument type="service" id="document.repository"/>
           <argument type="service" id="transmission.repository"/>
           <argument type="service" id="transmission_log.repository"/>
           <tag name="kernel.event_subscriber" />
           <tag name="kernel.event_listener" />
       </service>
   </services>
</container>

我还尝试通过这种方式直接转储 getInvoice()函数的结果:

$document = $this->docService->getInvoice($order);
            $contentD = $document['content'];
            var_dump($contentD);
            echo('<br>');
            if(strpos($contentD, 'Invoice') !== false){
                echo "Word Found!";
            } else{
                echo "Word Not Found!";
            }
            die('mem');

这是 $ contentD 的输出,在其中我看到单词“ 发票”,但 strpos 返回了“找不到单词”:< / p>

string(2759)“%PDF-1.3 1 0 obj << / Type / Catalog / Outlines 2 0 R /页3 0 R >> endobj 2 0 obj << / Type / Outlines / Count 0> > endobj 3 0 obj << / Type / Pages / Kids [6 0 R] / Count 1 /资源<< << / ProcSet 4 0 R / Font << / F1 8 0 R / F2 9 0 R >> / ExtGState << / GS1 10 0 R / GS2 11 0 R / GS3 12 0 R / GS4 13 0 R >> >> / MediaBox [0.000 0.000 595.280 841.890] >> endobj 4 0 obj [/ PDF / Text] endobj 5 0 obj << /生产者(domdom <8f49b3b0> + CPDF)/ CreationDate(D:20201110151124 + 00'00')/ ModDate(D:20201110151124 + 00'00')/标题(��发票1075 >)>> endobj 6 0 obj << / Type / Page / MediaBox [0.000 0.000 595.280 841.890] / Parent 3 0 R / Contents 7 0 R >> endobj 7 0 obj << / Filter / FlateDecode / Length 1124 >>流x�}V�r�6��+����AR7�%+�k�8I�bfhs�"9*���䔃�,�1<�U ^��� p����fq���Q��=��x�J�SiƅI� %DF0i�n��(��m o�z�o�Q$4��w$t̸Nv+ Eo%b:�9�/ ��'〜[lp��L�... ^KH����?4uƠ��/�\MF�ɔ�d�Tҧ�_ٲ����}q���j。 ��L����Vvڍ��K�AAr�M�S;R��)SQ�_QQ PK93 。 ��U��[��)\�vu�+�'<7*�,�O��:�#�_-p@�1vQ��Am�A��X�c:��J。 �SBt���J��L��1�+���A���A�My�g��〜��\g7F�b�o�> x B. a4��n��i��n��B�?)%(�(+�l���3��s<��v��é�H�>��W��Y。 eN�B��5.otilemڶup��ţ,¿=?{8��+�����N@��l�?���C.d���� s.n.6.g] ^ W。[S!5v.pL.S.yy.��{j {��=f��nK[U.3。 B =q������t��t�exB��CҠ����##����C,m�eNMb�G��)5��__��_。 -mS�]�(�NO�a�; [N]K�P�T�b��KE�9'.��S��M�8�td�w �4Rq�#&“B�AnH$〜Ӕ��hwh��dMzi;w�~~ޓoo}.�^^ {L�|u�2X�u�B�J�WP�l�z���U�0_�6�����s�6sl�2g:F���H \�o�Ӂ�B�Vg�G�%���2,�S��ЪX��U8]���A��M�<7Dȯ�,D�;̰ ��endstream endobj 8 0 obj << / Type / Font / Subtype / Type1 / Name / F1 / BaseFont / Helvetica / Encoding / WinAnsiEncoding >> endobj 9 0 obj << / Type / Font / Subtype / Type1 / Name / F2 / BaseFont / Helvetica-Bold / Encoding / WinAnsiEncoding >> endobj 10 0 obj << / Type / ExtGState / BM / Normal / ca 0.15 >> endobj 11 0 obj << / Type / ExtGState / BM / Normal / CA 0.15 >> endobj 12 0 obj << / Type / ExtGState / BM / Normal / ca 1 >> endobj 13 0 obj << / Type / ExtGState / BM / Normal / CA 1 >> endobj xref 0 14 0000000000 65 535 f 0000000009 00000 n 0000000074 00000 n 0000000120 00000 n 0000000350 00000 n 0000000379 00000 n 0000000574 00000 n 0000000677 00000 n 0000001874 00000 n 0000001981 00000 n 0000002093 00000 n 0000002153 00000 n 0000002213 00000 n 0000002270 00000 n拖车<< /大小14 /根1 0 R /信息5 0 R / ID [<27ce0894c6008fdc4303c0505f06206b> <27ce0894c6008fdc4303c0505f06206b>] >> startxref 2327 %% EOF“ * 找不到单词!内存

我是否忘记了一条use语句或services.xml中的某些内容,或者我使用了错误的存储库?我100%确信文档ID是正确的,我还尝试通过在数据库中手动获取一个ID(表“ document”)来获得相同的行为。

1 个答案:

答案 0 :(得分:0)

我发现了我的错误,我以错误的方式获取存储库。 这是获得它的正确方法:

/* @var EntityRepositoryInterface $docRepository */ 
$docRepository = $this->container->get('document.repository');

最后,这就是我获取发票编号的方式:

$document = $this->docService->getInvoice($order);

/* @var EntityRepositoryInterface $docRepository */
$docRepository = $this->container->get('document.repository');
$documentObject = $docRepository->search(new Criteria([ $document['id'] ]), $event->getContext());
$documentNumber = $documentObject->first()->getConfig()['custom']['invoiceNumber'];