AssociationBuilder.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Mapping\Builder;
  20. use Doctrine\ORM\Mapping\ClassMetadata;
  21. class AssociationBuilder
  22. {
  23. /**
  24. * @var ClassMetadataBuilder
  25. */
  26. protected $builder;
  27. /**
  28. * @var array
  29. */
  30. protected $mapping;
  31. /**
  32. * @var array
  33. */
  34. protected $joinColumns;
  35. /**
  36. *
  37. * @var int
  38. */
  39. protected $type;
  40. /**
  41. * @param ClassMetadataBuilder $builder
  42. * @param array $mapping
  43. */
  44. public function __construct(ClassMetadataBuilder $builder, array $mapping, $type)
  45. {
  46. $this->builder = $builder;
  47. $this->mapping = $mapping;
  48. $this->type = $type;
  49. }
  50. public function mappedBy($fieldName)
  51. {
  52. $this->mapping['mappedBy'] = $fieldName;
  53. return $this;
  54. }
  55. public function inversedBy($fieldName)
  56. {
  57. $this->mapping['inversedBy'] = $fieldName;
  58. return $this;
  59. }
  60. public function cascadeAll()
  61. {
  62. $this->mapping['cascade'] = array("ALL");
  63. return $this;
  64. }
  65. public function cascadePersist()
  66. {
  67. $this->mapping['cascade'][] = "persist";
  68. return $this;
  69. }
  70. public function cascadeRemove()
  71. {
  72. $this->mapping['cascade'][] = "remove";
  73. return $this;
  74. }
  75. public function cascadeMerge()
  76. {
  77. $this->mapping['cascade'][] = "merge";
  78. return $this;
  79. }
  80. public function cascadeDetach()
  81. {
  82. $this->mapping['cascade'][] = "detach";
  83. return $this;
  84. }
  85. public function cascadeRefresh()
  86. {
  87. $this->mapping['cascade'][] = "refresh";
  88. return $this;
  89. }
  90. public function fetchExtraLazy()
  91. {
  92. $this->mapping['fetch'] = ClassMetadata::FETCH_EXTRA_LAZY;
  93. return $this;
  94. }
  95. public function fetchEager()
  96. {
  97. $this->mapping['fetch'] = ClassMetadata::FETCH_EAGER;
  98. return $this;
  99. }
  100. public function fetchLazy()
  101. {
  102. $this->mapping['fetch'] = ClassMetadata::FETCH_LAZY;
  103. return $this;
  104. }
  105. /**
  106. * Add Join Columns
  107. *
  108. * @param string $columnName
  109. * @param string $referencedColumnName
  110. * @param bool $nullable
  111. * @param bool $unique
  112. * @param string $onDelete
  113. * @param string $columnDef
  114. */
  115. public function addJoinColumn($columnName, $referencedColumnName, $nullable = true, $unique = false, $onDelete = null, $columnDef = null)
  116. {
  117. $this->joinColumns[] = array(
  118. 'name' => $columnName,
  119. 'referencedColumnName' => $referencedColumnName,
  120. 'nullable' => $nullable,
  121. 'unique' => $unique,
  122. 'onDelete' => $onDelete,
  123. 'columnDefinition' => $columnDef,
  124. );
  125. return $this;
  126. }
  127. /**
  128. * @return ClassMetadataBuilder
  129. */
  130. public function build()
  131. {
  132. $mapping = $this->mapping;
  133. if ($this->joinColumns) {
  134. $mapping['joinColumns'] = $this->joinColumns;
  135. }
  136. $cm = $this->builder->getClassMetadata();
  137. if ($this->type == ClassMetadata::MANY_TO_ONE) {
  138. $cm->mapManyToOne($mapping);
  139. } else if ($this->type == ClassMetadata::ONE_TO_ONE) {
  140. $cm->mapOneToOne($mapping);
  141. } else {
  142. throw new \InvalidArgumentException("Type should be a ToOne Assocation here");
  143. }
  144. return $this->builder;
  145. }
  146. }