DDC522Test.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\Tests\Models\Company\CompanyPerson;
  4. require_once __DIR__ . '/../../../TestInit.php';
  5. /**
  6. * Tests that join columns (foreign keys) can be named the same as the association
  7. * fields they're used on without causing issues.
  8. */
  9. class DDC522Test extends \Doctrine\Tests\OrmFunctionalTestCase
  10. {
  11. protected function setUp()
  12. {
  13. parent::setUp();
  14. try {
  15. $this->_schemaTool->createSchema(array(
  16. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC522Customer'),
  17. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC522Cart'),
  18. $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC522ForeignKeyTest')
  19. ));
  20. } catch(\Exception $e) {
  21. }
  22. }
  23. /**
  24. * @group DDC-522
  25. */
  26. public function testJoinColumnWithSameNameAsAssociationField()
  27. {
  28. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  29. $cust = new DDC522Customer;
  30. $cust->name = "name";
  31. $cart = new DDC522Cart;
  32. $cart->total = 0;
  33. $cust->cart = $cart;
  34. $cart->customer = $cust;
  35. $this->_em->persist($cust);
  36. $this->_em->persist($cart);
  37. $this->_em->flush();
  38. $this->_em->clear();
  39. $r = $this->_em->createQuery("select ca,c from ".get_class($cart)." ca join ca.customer c")
  40. ->getResult();
  41. $this->assertInstanceOf(__NAMESPACE__ . '\DDC522Cart', $r[0]);
  42. $this->assertInstanceOf(__NAMESPACE__ . '\DDC522Customer', $r[0]->customer);
  43. $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $r[0]->customer);
  44. $this->assertEquals('name', $r[0]->customer->name);
  45. $fkt = new DDC522ForeignKeyTest();
  46. $fkt->cartId = $r[0]->id; // ignored for persistence
  47. $fkt->cart = $r[0]; // must be set properly
  48. $this->_em->persist($fkt);
  49. $this->_em->flush();
  50. $this->_em->clear();
  51. $fkt2 = $this->_em->find(get_class($fkt), $fkt->id);
  52. $this->assertEquals($fkt->cart->id, $fkt2->cartId);
  53. $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $fkt2->cart);
  54. $this->assertFalse($fkt2->cart->__isInitialized__);
  55. }
  56. /**
  57. * @group DDC-522
  58. * @group DDC-762
  59. */
  60. public function testJoinColumnWithNullSameNameAssociationField()
  61. {
  62. $fkCust = new DDC522ForeignKeyTest;
  63. $fkCust->name = "name";
  64. $fkCust->cart = null;
  65. $this->_em->persist($fkCust);
  66. $this->_em->flush();
  67. $this->_em->clear();
  68. $newCust = $this->_em->find(get_class($fkCust), $fkCust->id);
  69. }
  70. }
  71. /** @Entity */
  72. class DDC522Customer {
  73. /** @Id @Column(type="integer") @GeneratedValue */
  74. public $id;
  75. /** @Column */
  76. public $name;
  77. /** @OneToOne(targetEntity="DDC522Cart", mappedBy="customer") */
  78. public $cart;
  79. }
  80. /** @Entity */
  81. class DDC522Cart {
  82. /** @Id @Column(type="integer") @GeneratedValue */
  83. public $id;
  84. /** @Column(type="integer") */
  85. public $total;
  86. /**
  87. * @OneToOne(targetEntity="DDC522Customer", inversedBy="cart")
  88. * @JoinColumn(name="customer", referencedColumnName="id")
  89. */
  90. public $customer;
  91. }
  92. /** @Entity */
  93. class DDC522ForeignKeyTest {
  94. /** @Id @Column(type="integer") @GeneratedValue */
  95. public $id;
  96. /** @Column(type="integer", name="cart_id", nullable=true) */
  97. public $cartId;
  98. /**
  99. * @OneToOne(targetEntity="DDC522Cart")
  100. * @JoinColumn(name="cart_id", referencedColumnName="id")
  101. */
  102. public $cart;
  103. }