1: <?php
2:
3: namespace Alchemy\core\schema;
4:
5:
6: /**
7: * Class for representing a foreign key constraint in SQL
8: */
9: class ForeignKey extends Index {
10: protected static $default_args = array(
11: array(), array(),
12: 'ondelete' => 'restrict',
13: 'onupdate' => 'restrict');
14:
15: protected $sources;
16:
17:
18: /**
19: * Get the table the key references
20: *
21: * @return Table
22: */
23: public function getSourceTable() {
24: $this->resolve();
25: return $this->sources[0]->getTable();
26: }
27:
28:
29: /**
30: * Get the columns the key references
31: *
32: * @return array
33: */
34: public function listSources() {
35: $this->resolve();
36: return $this->sources;
37: }
38:
39:
40: public function getNameMap() {
41: $this->resolve();
42:
43: $map = array();
44: foreach($this->sources as $k => $source) {
45: $map[$source->getName()] = $this->columns[$k]->getName();
46: }
47:
48: return $map;
49: }
50:
51:
52: protected function resolve() {
53: if ($this->columns) return;
54:
55: parent::resolve();
56:
57: if (!isset($this->args[1]) || count($this->args[0]) != count($this->args[1])) {
58: throw new \Exception("ForeignKey received the wrong number of sources.");
59: }
60:
61: foreach($this->args[1] as $source) {
62: $this->sources[] = is_string($source)
63: ? Column::find($source, $this->table)
64: : $source;
65: }
66: }
67: }
68: