I added an extension attribute to the Invoice
class, and I want to be able to query the Invoice
database data by my extension attribute.
My extension attribute (ExportedFlag
) has these properties
entity_id
The primary keyinvoice_id
The foreign key to the invoice table.isExported
A string value.
When an invoice gets created, it will not have a corresponding row in my extension attribute table. I want to be able to query the database for a list of invoices that don't have a value for the extension attribute, using the API (and then insert the value for that extension attribute).
Here's the code that I have right now that adds the extension attribute to invoices.
public function afterGetList
(
\Magento\Sales\Api\InvoiceRepositoryInterface $subject,
\Magento\Sales\Model\ResourceModel\Order\Invoice\Collection $searchResult
) {
/** @var \Magento\Catalog\Api\Data\InvoiceInterface $invoice */
foreach ($searchResult as $invoice) {
$this->addExportedFlagToInvoice($invoice);
}
return $searchResult;
}
private function addExportedFlagToInvoice(\Magento\Sales\Api\Data\InvoiceInterface $invoice)
{
$extensionAttributes = $invoice->getExtensionAttributes();
if (empty($extensionAttributes)) {
$extensionAttributes = $this->invoiceExtensionFactory->create();
}
$exportedFlag = $this->exportedFlagProvider->getExportedFlag($invoice->getEntityId());
$extensionAttributes->setExportedFlag($exportedFlag);
$invoice->setExtensionAttributes($extensionAttributes);
$exportedFlag = $extensionAttributes->getExportedFlag();
$exportedFlag->setInvoiceId($invoice->getEntityId());
$this->entityManager->save($exportedFlag);
return $this;
}
The problem is that this code iterates through each invoice, and adds the extension attribute one-by-one. Furthermore, it retrieves all invoices, regardless of whether or not they have a value for the extension attribute (I don't want to retrieve values that have a row in the extension attributes table).
I want to be able to retrieve all invoices in a single query. Something like this
SELECT *
FROM sales_order_invoice
LEFT OUTER JOIN extensionAttribute
ON sales_order_invoice.entity_id = extensionAttribute.invoice_id
WHERE extensionAttribute.isExported IS NULL
How can I do this within Magento?