DDC353Test.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\ORM\UnitOfWork;
  4. require_once __DIR__ . '/../../../TestInit.php';
  5. class DDC353Test extends \Doctrine\Tests\OrmFunctionalTestCase
  6. {
  7. protected function setUp()
  8. {
  9. parent::setUp();
  10. try {
  11. $this->_schemaTool->createSchema(array(
  12. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC353File'),
  13. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC353Picture'),
  14. ));
  15. } catch(\Exception $ignored) {}
  16. }
  17. public function testWorkingCase()
  18. {
  19. $file = new DDC353File;
  20. $picture = new DDC353Picture;
  21. $picture->setFile($file);
  22. $em = $this->_em;
  23. $em->persist($picture);
  24. $em->flush();
  25. $em->clear();
  26. $fileId = $file->getFileId();
  27. $this->assertTrue($fileId > 0);
  28. $file = $em->getReference('Doctrine\Tests\ORM\Functional\Ticket\DDC353File', $fileId);
  29. $this->assertEquals(UnitOfWork::STATE_MANAGED, $em->getUnitOfWork()->getEntityState($file), "Reference Proxy should be marked MANAGED.");
  30. $picture = $em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC353Picture', $picture->getPictureId());
  31. $this->assertEquals(UnitOfWork::STATE_MANAGED, $em->getUnitOfWork()->getEntityState($picture->getFile()), "Lazy Proxy should be marked MANAGED.");
  32. $em->remove($picture);
  33. $em->flush();
  34. }
  35. public function testFailingCase()
  36. {
  37. $file = new DDC353File;
  38. $picture = new DDC353Picture;
  39. $picture->setFile($file);
  40. $em = $this->_em;
  41. $em->persist($picture);
  42. $em->flush();
  43. $em->clear();
  44. $fileId = $file->getFileId();
  45. $pictureId = $picture->getPictureId();
  46. $this->assertTrue($fileId > 0);
  47. $picture = $em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC353Picture', $pictureId);
  48. $this->assertEquals(UnitOfWork::STATE_MANAGED, $em->getUnitOfWork()->getEntityState($picture->getFile()), "Lazy Proxy should be marked MANAGED.");
  49. $em->remove($picture);
  50. $em->flush();
  51. }
  52. }
  53. /**
  54. * @Entity
  55. */
  56. class DDC353Picture
  57. {
  58. /**
  59. * @Column(name="picture_id", type="integer")
  60. * @Id @GeneratedValue
  61. */
  62. private $pictureId;
  63. /**
  64. * @ManyToOne(targetEntity="DDC353File", cascade={"persist", "remove"})
  65. * @JoinColumns({
  66. * @JoinColumn(name="file_id", referencedColumnName="file_id")
  67. * })
  68. */
  69. private $file;
  70. /**
  71. * Get pictureId
  72. */
  73. public function getPictureId()
  74. {
  75. return $this->pictureId;
  76. }
  77. /**
  78. * Set product
  79. */
  80. public function setProduct($value)
  81. {
  82. $this->product = $value;
  83. }
  84. /**
  85. * Get product
  86. */
  87. public function getProduct()
  88. {
  89. return $this->product;
  90. }
  91. /**
  92. * Set file
  93. */
  94. public function setFile($value)
  95. {
  96. $this->file = $value;
  97. }
  98. /**
  99. * Get file
  100. */
  101. public function getFile()
  102. {
  103. return $this->file;
  104. }
  105. }
  106. /**
  107. * @Entity
  108. */
  109. class DDC353File
  110. {
  111. /**
  112. * @Column(name="file_id", type="integer")
  113. * @Id
  114. * @GeneratedValue(strategy="AUTO")
  115. */
  116. public $fileId;
  117. /**
  118. * Get fileId
  119. */
  120. public function getFileId()
  121. {
  122. return $this->fileId;
  123. }
  124. }