ForeignKeyConstraint.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\DBAL\Schema;
  22. use Doctrine\DBAL\Schema\Visitor\Visitor;
  23. class ForeignKeyConstraint extends AbstractAsset implements Constraint
  24. {
  25. /**
  26. * @var Table
  27. */
  28. protected $_localTable;
  29. /**
  30. * @var array
  31. */
  32. protected $_localColumnNames;
  33. /**
  34. * @var string
  35. */
  36. protected $_foreignTableName;
  37. /**
  38. * @var array
  39. */
  40. protected $_foreignColumnNames;
  41. /**
  42. * @var string
  43. */
  44. protected $_cascade = '';
  45. /**
  46. * @var array
  47. */
  48. protected $_options;
  49. /**
  50. *
  51. * @param array $localColumnNames
  52. * @param string $foreignTableName
  53. * @param array $foreignColumnNames
  54. * @param string $cascade
  55. * @param string|null $name
  56. */
  57. public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name=null, array $options=array())
  58. {
  59. $this->_setName($name);
  60. $this->_localColumnNames = $localColumnNames;
  61. $this->_foreignTableName = $foreignTableName;
  62. $this->_foreignColumnNames = $foreignColumnNames;
  63. $this->_options = $options;
  64. }
  65. /**
  66. * @return string
  67. */
  68. public function getLocalTableName()
  69. {
  70. return $this->_localTable->getName();
  71. }
  72. /**
  73. * @param Table $table
  74. */
  75. public function setLocalTable(Table $table)
  76. {
  77. $this->_localTable = $table;
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function getLocalColumns()
  83. {
  84. return $this->_localColumnNames;
  85. }
  86. public function getColumns()
  87. {
  88. return $this->_localColumnNames;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getForeignTableName()
  94. {
  95. return $this->_foreignTableName;
  96. }
  97. /**
  98. * @return array
  99. */
  100. public function getForeignColumns()
  101. {
  102. return $this->_foreignColumnNames;
  103. }
  104. public function hasOption($name)
  105. {
  106. return isset($this->_options[$name]);
  107. }
  108. public function getOption($name)
  109. {
  110. return $this->_options[$name];
  111. }
  112. /**
  113. * Foreign Key onUpdate status
  114. *
  115. * @return string|null
  116. */
  117. public function onUpdate()
  118. {
  119. return $this->_onEvent('onUpdate');
  120. }
  121. /**
  122. * Foreign Key onDelete status
  123. *
  124. * @return string|null
  125. */
  126. public function onDelete()
  127. {
  128. return $this->_onEvent('onDelete');
  129. }
  130. /**
  131. * @param string $event
  132. * @return string|null
  133. */
  134. private function _onEvent($event)
  135. {
  136. if (isset($this->_options[$event])) {
  137. $onEvent = strtoupper($this->_options[$event]);
  138. if (!in_array($onEvent, array('NO ACTION', 'RESTRICT'))) {
  139. return $onEvent;
  140. }
  141. }
  142. return false;
  143. }
  144. }