【问题标题】:Shopware : get the document number by its IDShopware : 通过 ID 获取文件编号
【发布时间】:2020-11-10 16:26:34
【问题描述】:

我需要通过 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 的输出,我在其中看到了“Invoice”这个词,但 strpos 返回了“Word not found”:

string(2759) "%PDF-1.3 1 0 obj > endobj 2 0 obj > endobj 3 0 obj > /ExtGState > >> /MediaBox [0.000 0.000 595.280 841.890] >> endobj 4 0 obj [/PDF /Text] endobj 5 0 obj + CPDF) /CreationDate (D:20201110151124+00'00') /ModDate (D:20201110151124+00'00') /Title (��Invoice 1075) >> endobj 6 0 obj > endobj 7 0 obj > 流x�}V�r�6��+����AR7�%+�k�8I�bfhs�"9*���䔃�,�1&lt;�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�_�Q�PK93���U ��[��)\�vu�+�'�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�A��X �c:��J��SBt����J ��L�� 1�+��A���ۍA�My�g��~����\g7F�b�۟�o�>xB�a4��n��i��n� �B�?)%(�(+�l����3��s��W��Y�eN�B��5.o0m��向上���� ,... ��[S!5v�pL���S����y������{j{��=f��nK[U��3�ƻ=q������t��t” exB��CҠ����#֑���C، m�eNMb�G��)5���_��_�-mS�]�(�NO�a��;[ 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�> endobj 9 0 obj > endobj 10 0 obj > endobj 11 0 obj > endobj 12 0 obj > endobj 13 0 obj > endobj 外部参照 0 14 0000000000 655 35女性0000000009 00000Ñ0000000074 00000Ñ0000000120 00000Ñ0000000350 00000Ñ0000000379 00000Ñ0000000574 00000Ñ0000000677 00000Ñ0000001874 00000Ñ0000001981 00000Ñ0000002093 00000Ñ0000002153 00000Ñ0000002213 00000Ñ0000002270 00000Ñ拖车] >> startxref 2327 %%EOF "* 找不到单词!内存

我是否忘记了 services.xml 中的 use 声明或其他内容,还是我使用了错误的存储库?我 100% 确定文档的 ID 是正确的,我还尝试在数据库中手动获取 ID(表“文档”)并得到相同的行为。

【问题讨论】:

    标签: php symfony shopware


    【解决方案1】:

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

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

    最后,这就是我得到 invoiceNumber 的方式:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多