ManyToManyAssociationBuilder.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /**
  21. * ManyToMany Association Builder
  22. *
  23. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  24. * @link www.doctrine-project.com
  25. * @since 2.0
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. */
  28. class ManyToManyAssociationBuilder extends OneToManyAssociationBuilder
  29. {
  30. private $joinTableName;
  31. private $inverseJoinColumns = array();
  32. public function setJoinTable($name)
  33. {
  34. $this->joinTableName = $name;
  35. return $this;
  36. }
  37. /**
  38. * Add Inverse Join Columns
  39. *
  40. * @param string $columnName
  41. * @param string $referencedColumnName
  42. * @param bool $nullable
  43. * @param bool $unique
  44. * @param string $onDelete
  45. * @param string $columnDef
  46. */
  47. public function addInverseJoinColumn($columnName, $referencedColumnName, $nullable = true, $unique = false, $onDelete = null, $columnDef = null)
  48. {
  49. $this->inverseJoinColumns[] = array(
  50. 'name' => $columnName,
  51. 'referencedColumnName' => $referencedColumnName,
  52. 'nullable' => $nullable,
  53. 'unique' => $unique,
  54. 'onDelete' => $onDelete,
  55. 'columnDefinition' => $columnDef,
  56. );
  57. return $this;
  58. }
  59. /**
  60. * @return ClassMetadataBuilder
  61. */
  62. public function build()
  63. {
  64. $mapping = $this->mapping;
  65. $mapping['joinTable'] = array();
  66. if ($this->joinColumns) {
  67. $mapping['joinTable']['joinColumns'] = $this->joinColumns;
  68. }
  69. if ($this->inverseJoinColumns) {
  70. $mapping['joinTable']['inverseJoinColumns'] = $this->inverseJoinColumns;
  71. }
  72. if ($this->joinTableName) {
  73. $mapping['joinTable']['name'] = $this->joinTableName;
  74. }
  75. $cm = $this->builder->getClassMetadata();
  76. $cm->mapManyToMany($mapping);
  77. return $this->builder;
  78. }
  79. }