ForeignKeyConstraint.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace Doctrine\DBAL\Schema;
  3. use Doctrine\DBAL\Platforms\AbstractPlatform;
  4. use function array_combine;
  5. use function array_keys;
  6. use function array_map;
  7. use function end;
  8. use function explode;
  9. use function in_array;
  10. use function strtolower;
  11. use function strtoupper;
  12. /**
  13. * An abstraction class for a foreign key constraint.
  14. */
  15. class ForeignKeyConstraint extends AbstractAsset implements Constraint
  16. {
  17. /**
  18. * Instance of the referencing table the foreign key constraint is associated with.
  19. *
  20. * @var Table
  21. */
  22. protected $_localTable;
  23. /**
  24. * Asset identifier instances of the referencing table column names the foreign key constraint is associated with.
  25. * array($columnName => Identifier)
  26. *
  27. * @var Identifier[]
  28. */
  29. protected $_localColumnNames;
  30. /**
  31. * Table or asset identifier instance of the referenced table name the foreign key constraint is associated with.
  32. *
  33. * @var Table|Identifier
  34. */
  35. protected $_foreignTableName;
  36. /**
  37. * Asset identifier instances of the referenced table column names the foreign key constraint is associated with.
  38. * array($columnName => Identifier)
  39. *
  40. * @var Identifier[]
  41. */
  42. protected $_foreignColumnNames;
  43. /**
  44. * Options associated with the foreign key constraint.
  45. *
  46. * @var mixed[]
  47. */
  48. protected $_options;
  49. /**
  50. * Initializes the foreign key constraint.
  51. *
  52. * @param string[] $localColumnNames Names of the referencing table columns.
  53. * @param Table|string $foreignTableName Referenced table.
  54. * @param string[] $foreignColumnNames Names of the referenced table columns.
  55. * @param string|null $name Name of the foreign key constraint.
  56. * @param mixed[] $options Options associated with the foreign key constraint.
  57. */
  58. public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name = null, array $options = [])
  59. {
  60. $this->_setName($name);
  61. $identifierConstructorCallback = static function ($column) {
  62. return new Identifier($column);
  63. };
  64. $this->_localColumnNames = $localColumnNames
  65. ? array_combine($localColumnNames, array_map($identifierConstructorCallback, $localColumnNames))
  66. : [];
  67. if ($foreignTableName instanceof Table) {
  68. $this->_foreignTableName = $foreignTableName;
  69. } else {
  70. $this->_foreignTableName = new Identifier($foreignTableName);
  71. }
  72. $this->_foreignColumnNames = $foreignColumnNames
  73. ? array_combine($foreignColumnNames, array_map($identifierConstructorCallback, $foreignColumnNames))
  74. : [];
  75. $this->_options = $options;
  76. }
  77. /**
  78. * Returns the name of the referencing table
  79. * the foreign key constraint is associated with.
  80. *
  81. * @return string
  82. */
  83. public function getLocalTableName()
  84. {
  85. return $this->_localTable->getName();
  86. }
  87. /**
  88. * Sets the Table instance of the referencing table
  89. * the foreign key constraint is associated with.
  90. *
  91. * @param Table $table Instance of the referencing table.
  92. *
  93. * @return void
  94. */
  95. public function setLocalTable(Table $table)
  96. {
  97. $this->_localTable = $table;
  98. }
  99. /**
  100. * @return Table
  101. */
  102. public function getLocalTable()
  103. {
  104. return $this->_localTable;
  105. }
  106. /**
  107. * Returns the names of the referencing table columns
  108. * the foreign key constraint is associated with.
  109. *
  110. * @return string[]
  111. */
  112. public function getLocalColumns()
  113. {
  114. return array_keys($this->_localColumnNames);
  115. }
  116. /**
  117. * Returns the quoted representation of the referencing table column names
  118. * the foreign key constraint is associated with.
  119. *
  120. * But only if they were defined with one or the referencing table column name
  121. * is a keyword reserved by the platform.
  122. * Otherwise the plain unquoted value as inserted is returned.
  123. *
  124. * @param AbstractPlatform $platform The platform to use for quotation.
  125. *
  126. * @return string[]
  127. */
  128. public function getQuotedLocalColumns(AbstractPlatform $platform)
  129. {
  130. $columns = [];
  131. foreach ($this->_localColumnNames as $column) {
  132. $columns[] = $column->getQuotedName($platform);
  133. }
  134. return $columns;
  135. }
  136. /**
  137. * Returns unquoted representation of local table column names for comparison with other FK
  138. *
  139. * @return string[]
  140. */
  141. public function getUnquotedLocalColumns()
  142. {
  143. return array_map([$this, 'trimQuotes'], $this->getLocalColumns());
  144. }
  145. /**
  146. * Returns unquoted representation of foreign table column names for comparison with other FK
  147. *
  148. * @return string[]
  149. */
  150. public function getUnquotedForeignColumns()
  151. {
  152. return array_map([$this, 'trimQuotes'], $this->getForeignColumns());
  153. }
  154. /**
  155. * {@inheritdoc}
  156. *
  157. * @see getLocalColumns
  158. */
  159. public function getColumns()
  160. {
  161. return $this->getLocalColumns();
  162. }
  163. /**
  164. * Returns the quoted representation of the referencing table column names
  165. * the foreign key constraint is associated with.
  166. *
  167. * But only if they were defined with one or the referencing table column name
  168. * is a keyword reserved by the platform.
  169. * Otherwise the plain unquoted value as inserted is returned.
  170. *
  171. * @see getQuotedLocalColumns
  172. *
  173. * @param AbstractPlatform $platform The platform to use for quotation.
  174. *
  175. * @return string[]
  176. */
  177. public function getQuotedColumns(AbstractPlatform $platform)
  178. {
  179. return $this->getQuotedLocalColumns($platform);
  180. }
  181. /**
  182. * Returns the name of the referenced table
  183. * the foreign key constraint is associated with.
  184. *
  185. * @return string
  186. */
  187. public function getForeignTableName()
  188. {
  189. return $this->_foreignTableName->getName();
  190. }
  191. /**
  192. * Returns the non-schema qualified foreign table name.
  193. *
  194. * @return string
  195. */
  196. public function getUnqualifiedForeignTableName()
  197. {
  198. $parts = explode('.', $this->_foreignTableName->getName());
  199. return strtolower(end($parts));
  200. }
  201. /**
  202. * Returns the quoted representation of the referenced table name
  203. * the foreign key constraint is associated with.
  204. *
  205. * But only if it was defined with one or the referenced table name
  206. * is a keyword reserved by the platform.
  207. * Otherwise the plain unquoted value as inserted is returned.
  208. *
  209. * @param AbstractPlatform $platform The platform to use for quotation.
  210. *
  211. * @return string
  212. */
  213. public function getQuotedForeignTableName(AbstractPlatform $platform)
  214. {
  215. return $this->_foreignTableName->getQuotedName($platform);
  216. }
  217. /**
  218. * Returns the names of the referenced table columns
  219. * the foreign key constraint is associated with.
  220. *
  221. * @return string[]
  222. */
  223. public function getForeignColumns()
  224. {
  225. return array_keys($this->_foreignColumnNames);
  226. }
  227. /**
  228. * Returns the quoted representation of the referenced table column names
  229. * the foreign key constraint is associated with.
  230. *
  231. * But only if they were defined with one or the referenced table column name
  232. * is a keyword reserved by the platform.
  233. * Otherwise the plain unquoted value as inserted is returned.
  234. *
  235. * @param AbstractPlatform $platform The platform to use for quotation.
  236. *
  237. * @return string[]
  238. */
  239. public function getQuotedForeignColumns(AbstractPlatform $platform)
  240. {
  241. $columns = [];
  242. foreach ($this->_foreignColumnNames as $column) {
  243. $columns[] = $column->getQuotedName($platform);
  244. }
  245. return $columns;
  246. }
  247. /**
  248. * Returns whether or not a given option
  249. * is associated with the foreign key constraint.
  250. *
  251. * @param string $name Name of the option to check.
  252. *
  253. * @return bool
  254. */
  255. public function hasOption($name)
  256. {
  257. return isset($this->_options[$name]);
  258. }
  259. /**
  260. * Returns an option associated with the foreign key constraint.
  261. *
  262. * @param string $name Name of the option the foreign key constraint is associated with.
  263. *
  264. * @return mixed
  265. */
  266. public function getOption($name)
  267. {
  268. return $this->_options[$name];
  269. }
  270. /**
  271. * Returns the options associated with the foreign key constraint.
  272. *
  273. * @return mixed[]
  274. */
  275. public function getOptions()
  276. {
  277. return $this->_options;
  278. }
  279. /**
  280. * Returns the referential action for UPDATE operations
  281. * on the referenced table the foreign key constraint is associated with.
  282. *
  283. * @return string|null
  284. */
  285. public function onUpdate()
  286. {
  287. return $this->onEvent('onUpdate');
  288. }
  289. /**
  290. * Returns the referential action for DELETE operations
  291. * on the referenced table the foreign key constraint is associated with.
  292. *
  293. * @return string|null
  294. */
  295. public function onDelete()
  296. {
  297. return $this->onEvent('onDelete');
  298. }
  299. /**
  300. * Returns the referential action for a given database operation
  301. * on the referenced table the foreign key constraint is associated with.
  302. *
  303. * @param string $event Name of the database operation/event to return the referential action for.
  304. *
  305. * @return string|null
  306. */
  307. private function onEvent($event)
  308. {
  309. if (isset($this->_options[$event])) {
  310. $onEvent = strtoupper($this->_options[$event]);
  311. if (! in_array($onEvent, ['NO ACTION', 'RESTRICT'])) {
  312. return $onEvent;
  313. }
  314. }
  315. return false;
  316. }
  317. /**
  318. * Checks whether this foreign key constraint intersects the given index columns.
  319. *
  320. * Returns `true` if at least one of this foreign key's local columns
  321. * matches one of the given index's columns, `false` otherwise.
  322. *
  323. * @param Index $index The index to be checked against.
  324. *
  325. * @return bool
  326. */
  327. public function intersectsIndexColumns(Index $index)
  328. {
  329. foreach ($index->getColumns() as $indexColumn) {
  330. foreach ($this->_localColumnNames as $localColumn) {
  331. if (strtolower($indexColumn) === strtolower($localColumn->getName())) {
  332. return true;
  333. }
  334. }
  335. }
  336. return false;
  337. }
  338. }