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

Shopware : get the document number by its ID

我需要通过 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>

I also tried to directly dump the result of the getInvoice() function and this way:

$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');

This is the output of $contentD in which I see the word "Invoice" but strpos returned "Word not found" :

string(2759) "%PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /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 << /Producer (�dompdf <�8f49b3b0> + CPDF) /CreationDate (D:20201110151124+00'00') /ModDate (D:20201110151124+00'00') /Title (�发票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��(��mo�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����_��PK937*�,�O��:�#�_-p@�1vQ��A��:Ɗ�� (C��j��W��mגZ����2��d��h!-Zp��h��:��ᇒ)��,=��=O��dJ>����U��[��) \��u��+��'��<��T��99&��)��H��gL�|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��A��X ��c:��J��SBt����J��L� �1��+����A����6A.My.g��~����\g7F.b.5.o.>xB.a4..n..i..n.B.? )%(��(+��l����3��s<��v��é��>��W��Y��eN��B��5.om��Ƕup��ţ, ��=?{8��+������N@��l��?��ń��C��d���� ����s��n��6��g]^W���� [S!5v��pL����S����y������{j{��=f��nK[U��3����=q������t��t��exB ��C¡ N]K�P�T��b��KE �9'.�S�M�8�c�td �w�4Rq�#&"B�AnH$~Ӕ� hwh��dMzi;w����~��Γ��o�}��^�u��bZ 5iĸ$~�d��3Q�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 65535 f 00000000 09 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 trailer << /Size 14 /Root 1 0 R /信息 5 0 R /ID[<27ce0894c6008fdc4303c0505f06206b><27ce0894c6008fdc4303c0505f06206b>] >> startxref 2327 %%EOF "* 未找到单词!mem

Did I forgot a use statement or something in services.xml or am I using the wrong repository? I am 100% sure that the ID of document is correct, I also tried by taking an ID manually in the database (table "document") and got same behavior.

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

/* @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'];