ForeignKeyConstraint.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 MIT license. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\DBAL\Schema;
  22. use Doctrine\DBAL\Schema\Visitor\Visitor;
  23. use Doctrine\DBAL\Platforms\AbstractPlatform;
  24. class ForeignKeyConstraint extends AbstractAsset implements Constraint
  25. {
  26. /**
  27. * @var Table
  28. */
  29. protected $_localTable;
  30. /**
  31. * @var array
  32. */
  33. protected $_localColumnNames;
  34. /**
  35. * @var string
  36. */
  37. protected $_foreignTableName;
  38. /**
  39. * @var array
  40. */
  41. protected $_foreignColumnNames;
  42. /**
  43. * @var string
  44. */
  45. protected $_cascade = '';
  46. /**
  47. * @var array
  48. */
  49. protected $_options;
  50. /**
  51. *
  52. * @param array $localColumnNames
  53. * @param string $foreignTableName
  54. * @param array $foreignColumnNames
  55. * @param string $cascade
  56. * @param string|null $name
  57. */
  58. public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name=null, array $options=array())
  59. {
  60. $this->_setName($name);
  61. $this->_localColumnNames = $localColumnNames;
  62. $this->_foreignTableName = $foreignTableName;
  63. $this->_foreignColumnNames = $foreignColumnNames;
  64. $this->_options = $options;
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getLocalTableName()
  70. {
  71. return $this->_localTable->getName();
  72. }
  73. /**
  74. * @param Table $table
  75. */
  76. public function setLocalTable(Table $table)
  77. {
  78. $this->_localTable = $table;
  79. }
  80. /**
  81. * @return array
  82. */
  83. public function getLocalColumns()
  84. {
  85. return $this->_localColumnNames;
  86. }
  87. public function getColumns()
  88. {
  89. return $this->_localColumnNames;
  90. }
  91. /**
  92. * @return string
  93. */
  94. public function getForeignTableName()
  95. {
  96. return $this->_foreignTableName;
  97. }
  98. /**
  99. * Return the non-schema qualified foreign table name.
  100. *
  101. * @return string
  102. */
  103. public function getUnqualifiedForeignTableName()
  104. {
  105. $parts = explode(".", $this->_foreignTableName);
  106. return strtolower(end($parts));
  107. }
  108. /**
  109. * Get the quoted representation of this asset but only if it was defined with one. Otherwise
  110. * return the plain unquoted value as inserted.
  111. *
  112. * @param AbstractPlatform $platform
  113. * @return string
  114. */
  115. public function getQuotedForeignTableName(AbstractPlatform $platform)
  116. {
  117. $keywords = $platform->getReservedKeywordsList();
  118. $parts = explode(".", $this->getForeignTableName());
  119. foreach ($parts AS $k => $v) {
  120. $parts[$k] = ($this->_quoted || $keywords->isKeyword($v)) ? $platform->quoteIdentifier($v) : $v;
  121. }
  122. return implode(".", $parts);
  123. }
  124. /**
  125. * @return array
  126. */
  127. public function getForeignColumns()
  128. {
  129. return $this->_foreignColumnNames;
  130. }
  131. public function hasOption($name)
  132. {
  133. return isset($this->_options[$name]);
  134. }
  135. public function getOption($name)
  136. {
  137. return $this->_options[$name];
  138. }
  139. /**
  140. * Gets the options associated with this constraint
  141. *
  142. * @return array
  143. */
  144. public function getOptions()
  145. {
  146. return $this->_options;
  147. }
  148. /**
  149. * Foreign Key onUpdate status
  150. *
  151. * @return string|null
  152. */
  153. public function onUpdate()
  154. {
  155. return $this->_onEvent('onUpdate');
  156. }
  157. /**
  158. * Foreign Key onDelete status
  159. *
  160. * @return string|null
  161. */
  162. public function onDelete()
  163. {
  164. return $this->_onEvent('onDelete');
  165. }
  166. /**
  167. * @param string $event
  168. * @return string|null
  169. */
  170. private function _onEvent($event)
  171. {
  172. if (isset($this->_options[$event])) {
  173. $onEvent = strtoupper($this->_options[$event]);
  174. if (!in_array($onEvent, array('NO ACTION', 'RESTRICT'))) {
  175. return $onEvent;
  176. }
  177. }
  178. return false;
  179. }
  180. }