Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface MyExtensionInterface extends ExtensibleDataInterface
- {
- const ENTITY_ID = "entity_id";
- const INVOICE_ID = "invoice_id";
- // Foreign key to the sales_invoice table
- public function getInvoiceId();
- public function setInvoiceId();
- public function getMyValue();
- public function setMyValue();
- public function getExtensionAttributes();
- public function setExtensionAttributes(MyCompanyMyExtensionAttributeApiDataMyExtensionInterface $extensionAttributes);
- }
- class MyProvider implements MyExtensionProviderInterface
- {
- public function __construct
- (
- EntityManager $entityManager,
- Loader $loader,
- MyExtensionFactory $myExtensionFactory
- ) {
- $this->entityManager = $entityManager;
- $this->loader = $loader;
- $this->myExtensionFactory = $myExtensionFactory;
- }
- // This is the method where I want to update the extension attribute's
- // value, and then update it in the database
- public function setMyExtensionValue($invoiceId, $value)
- {
- $ids = $this->loader->getMyExtensionAttributeByInvoiceId($invoiceId);
- if (count($ids) > 0)
- {
- $id = $ids[0];
- $myExtension = $this->myExtensionFactory->create();
- $this->entityManager->load($myExtension, $id);
- }
- else
- {
- $myExtension = $this->myExtensionFactory->create();
- $myExtension->setInvoiceId($invoiceId);
- }
- $myExtension->setMyValue($value);
- // This is where I'm getting an unique constraint violation
- $this->entityManager->save($myExtension);
- }
- }
- class Loader
- {
- private $metadataPool;
- private $resourceConnection;
- public function __construct
- (
- MagentoFrameworkEntityManagerMetadataPool $metadataPool,
- ResourceConnection $resourceConnection
- ) {
- $this->metadataPool = $metadataPool;
- $this->resourceConnection = $resourceConnection;
- }
- public function getMyExtensionAttributeByInvoiceId($invoiceId)
- {
- $metadata = $this->metadataPool->getMetadata(MyExtensionInterface::class);
- $connection = $this->resourceConnection->getConnection();
- $select = $connection
- ->select()
- ->from($metadata->getEntityTable(), MyExtensionInterface::ENTITY_ID)
- ->where(MyExtensionInterface::INVOICE_ID . ' = ?', $invoiceId);
- $ids = $connection->fetchCol($select);
- return $ids ?: [];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement