Guest User

Untitled

a guest
Mar 21st, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. class Transaction
  2. {
  3. ...
  4.  
  5. /**
  6. *
  7. * @ORMOneToOne(targetEntity="Transaction")
  8. * @ORMJoinColumn(name="reverse_transaction_id", referencedColumnName="id")
  9. * @var FinanceiroEntityMovimentacao
  10. */
  11. private $reverseTransaction;
  12.  
  13. ...
  14. }
  15.  
  16. CREATE TABLE `transaction` (
  17. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  18. ...
  19. `reverse_transaction_id` bigint(20) unsigned DEFAULT NULL,
  20. ...
  21. PRIMARY KEY (`id`),
  22. ...
  23. KEY `fk_reverse_transaction_idx` (`reverse_transaction_id`),
  24. ...
  25. CONSTRAINT `fk_reverse_transaction` FOREIGN KEY (`reverse_transaction_id`) REFERENCES `transaction` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  26. ...
  27. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  28.  
  29. /**
  30. * Consider that hydrate() is a method that hydrates the entity
  31. * based on the passed $data and the reference to the entity manager
  32. */
  33.  
  34. // Creation of the first transaction
  35. $dataA = [
  36. ...
  37. ];
  38. $transactionA = new Transaction();
  39. $transactionA->hydrate($dataA, $entityManager);
  40. $entityManager->persist($transactionA);
  41. $entityManager->flush($transactionA);
  42.  
  43.  
  44. // Creation of the second transaction
  45. // Already pointing to the first one
  46. $dataB = [
  47. ...
  48. 'reverse_transaction' => $transactionA->getId()
  49. ...
  50. ];
  51. $transactionB = new Transaction();
  52. $transactionB->hydrate($dataB, $entityManager);
  53. $entityManager->persist($transactionB);
  54. $entityManager->flush($transactionB);
  55.  
  56.  
  57. // Update of the first transaction to also point
  58. // to the second one
  59. // The error occurs here
  60. $transactionA->setReverseTransaction($transactionB);
  61. $entityManager->persist($transactionA);
  62. $entityManager->flush($transactionA);
Add Comment
Please, Sign In to add comment