I need to get the documentNumber in the documentRepository by its ID. I am doing a “simple” DAL request to do this but for some reason dumping the output gives empty fields.
Here is my subscriber containing the DAL request :
<?php declare(strict_types=1); namespace HmDocumentGeneratorPluginSubscriber; use ShopwareCoreFrameworkDataAbstractionLayerEntityRepositoryInterface; use ShopwareCoreFrameworkDataAbstractionLayerSearchCriteria; use HmDocumentGeneratorPluginServiceHmDocumentService; use HmDocumentGeneratorPluginServiceHmOrderService; use ShopwareCoreCheckoutCartEventCheckoutOrderPlacedEvent; use HmDocumentGeneratorPluginEventInvoiceGeneratedEvent; use ShopwareCoreCheckoutOrderOrderEntity; use SymfonyComponentEventDispatcherEventSubscriberInterface; use SymfonyComponentDependencyInjectionContainerInterface; use ShopwareCoreCheckoutDocumentEntity; use ShopwareCoreFrameworkDataAbstractionLayerSearchFilterEqualsFilter; use EmakersTransmissionPluginTransmissionPlugin; use EmakersTransmissionPluginServicesExactDataService; 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'] ]), ShopwareCoreFrameworkContext::createDefaultContext() ); var_dump($documentObject); die('please man'); } } }
Here is my 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="HmDocumentGeneratorPluginSubscriberOrderSubscriber"> <argument type="service" id="service_container"/> <argument id="HmDocumentGeneratorPluginServiceHmOrderService" type="service" /> <argument id="HmDocumentGeneratorPluginServiceHmDocumentService" 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 (��Invoice 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 >> stream 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ܣ�_�Q� PK93<S��l�vH�ysq?���-�1Y��ƔjqD��Ε�j�3<�o�=��ʎ�3�0RHF�0�I��}���H’ �f=�U&3.�Y�-� ��a�=W��gpo�i�ක��h]��������7*�,�O��:�#�_-p@�1vQ��A
�:Ɗ�(C��j�W��mגZ���2�d�h!-Zp��h�:�ᇒ)�,=��=O��dJ>���U��[��)�v 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���ۍA�My�g��~����g7F�b�۟�o�>xB�a4��n��i��n��B�?)%(�(+�l���3��s<��v��é�H�>��W��Y�eN�B��5.oەm�ڶ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Ҡ����#֑���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�<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 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 trailer << /Size 14 /Root 1 0 R /Info 5 0 R /ID[<27ce0894c6008fdc4303c0505f06206b><27ce0894c6008fdc4303c0505f06206b>] >> startxref 2327 %%EOF “*
Word Not Found!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.
Advertisement
Answer
I found my error, I was getting the repository in a wrong way. Here is the correct way to get it :
/* @var EntityRepositoryInterface $docRepository */ $docRepository = $this->container->get('document.repository');
At the end, this is how I got the 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'];