Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. interface MyExtensionInterface extends ExtensibleDataInterface
  2. {
  3. const ENTITY_ID = "entity_id";
  4. const INVOICE_ID = "invoice_id";
  5.  
  6. // Foreign key to the sales_invoice table
  7. public function getInvoiceId();
  8. public function setInvoiceId();
  9.  
  10. public function getMyValue();
  11. public function setMyValue();
  12.  
  13. public function getExtensionAttributes();
  14. public function setExtensionAttributes(MyCompanyMyExtensionAttributeApiDataMyExtensionInterface $extensionAttributes);
  15. }
  16.  
  17. class MyProvider implements MyExtensionProviderInterface
  18. {
  19. public function __construct
  20. (
  21. EntityManager $entityManager,
  22. Loader $loader,
  23. MyExtensionFactory $myExtensionFactory
  24. ) {
  25. $this->entityManager = $entityManager;
  26. $this->loader = $loader;
  27. $this->myExtensionFactory = $myExtensionFactory;
  28. }
  29.  
  30. // This is the method where I want to update the extension attribute's
  31. // value, and then update it in the database
  32. public function setMyExtensionValue($invoiceId, $value)
  33. {
  34. $ids = $this->loader->getMyExtensionAttributeByInvoiceId($invoiceId);
  35.  
  36. if (count($ids) > 0)
  37. {
  38. $id = $ids[0];
  39. $myExtension = $this->myExtensionFactory->create();
  40. $this->entityManager->load($myExtension, $id);
  41. }
  42. else
  43. {
  44. $myExtension = $this->myExtensionFactory->create();
  45. $myExtension->setInvoiceId($invoiceId);
  46. }
  47.  
  48. $myExtension->setMyValue($value);
  49.  
  50. // This is where I'm getting an unique constraint violation
  51. $this->entityManager->save($myExtension);
  52. }
  53. }
  54.  
  55. class Loader
  56. {
  57. private $metadataPool;
  58. private $resourceConnection;
  59.  
  60. public function __construct
  61. (
  62. MagentoFrameworkEntityManagerMetadataPool $metadataPool,
  63. ResourceConnection $resourceConnection
  64. ) {
  65. $this->metadataPool = $metadataPool;
  66. $this->resourceConnection = $resourceConnection;
  67. }
  68.  
  69. public function getMyExtensionAttributeByInvoiceId($invoiceId)
  70. {
  71. $metadata = $this->metadataPool->getMetadata(MyExtensionInterface::class);
  72. $connection = $this->resourceConnection->getConnection();
  73. $select = $connection
  74. ->select()
  75. ->from($metadata->getEntityTable(), MyExtensionInterface::ENTITY_ID)
  76. ->where(MyExtensionInterface::INVOICE_ID . ' = ?', $invoiceId);
  77. $ids = $connection->fetchCol($select);
  78. return $ids ?: [];
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement