1<?php 2/** 3 * Phinx 4 * 5 * (The MIT license) 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated * documentation files (the "Software"), to 9 * deal in the Software without restriction, including without limitation the 10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 11 * sell copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 * IN THE SOFTWARE. 24 */ 25namespace Phinx\Db\Action; 26 27use Phinx\Db\Table\ForeignKey; 28use Phinx\Db\Table\Table; 29 30class AddForeignKey extends Action 31{ 32 33 /** 34 * The foreign key to add 35 * 36 * @var ForeignKey 37 */ 38 protected $foreignKey; 39 40 /** 41 * Constructor 42 * 43 * @param Table $table The table to add the foreign key to 44 * @param ForeignKey $fk The foreign key to add 45 */ 46 public function __construct(Table $table, ForeignKey $fk) 47 { 48 parent::__construct($table); 49 $this->foreignKey = $fk; 50 } 51 52 /** 53 * Creates a new AddForeignKey object after building the foreign key with 54 * the passed attributes 55 * 56 * @param Table $table The table object to add the foreign key to 57 * @param string|string[] $columns The columns for the foreign key 58 * @param Table|string $referencedTable The table the foreign key references 59 * @param string|array $referencedColumns The columns in the referenced table 60 * @param array $options Extra options for the foreign key 61 * @param string|null $name The name of the foreign key 62 * @return AddForeignKey 63 */ 64 public static function build(Table $table, $columns, $referencedTable, $referencedColumns = ['id'], array $options = [], $name = null) 65 { 66 if (is_string($referencedColumns)) { 67 $referencedColumns = [$referencedColumns]; // str to array 68 } 69 70 if (is_string($referencedTable)) { 71 $referencedTable = new Table($referencedTable); 72 } 73 74 $fk = new ForeignKey(); 75 $fk->setReferencedTable($referencedTable) 76 ->setColumns($columns) 77 ->setReferencedColumns($referencedColumns) 78 ->setOptions($options); 79 80 if ($name !== null) { 81 $fk->setConstraint($name); 82 } 83 84 return new static($table, $fk); 85 } 86 87 /** 88 * Returns the foreign key to be added 89 * 90 * @return ForeignKey 91 */ 92 public function getForeignKey() 93 { 94 return $this->foreignKey; 95 } 96} 97