1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yaoi\Database\Definition; |
4
|
|
|
|
5
|
|
|
use Yaoi\String\Utils; |
6
|
|
|
|
7
|
|
|
class Columns |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Column[] |
11
|
|
|
*/ |
12
|
|
|
private $_arrayOfColumnData = array(); |
13
|
|
|
/** |
14
|
|
|
* @var Table |
15
|
|
|
*/ |
16
|
|
|
private $table; |
17
|
|
|
|
18
|
|
|
public function __construct(Table $table) |
19
|
|
|
{ |
20
|
|
|
$this->table = $table; |
21
|
|
|
$table->columns = $this; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function __set($name, $column) |
25
|
|
|
{ |
26
|
|
|
if (is_int($column)) { |
27
|
|
|
$column = new Column($column); |
28
|
|
|
//$this->_arrayOfColumnData[$name] = $column; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// another column reference |
32
|
|
|
if (!empty($column->table) |
33
|
|
|
&& $column->table->schemaName != $this->table->schemaName) { |
34
|
|
|
$refColumn = $column; |
35
|
|
|
$column = clone $column; |
36
|
|
|
|
37
|
|
|
$column->propertyName = $name; |
38
|
|
|
$column->schemaName = Utils::fromCamelCase($name); |
39
|
|
|
$column->table = $this->table; |
40
|
|
|
|
41
|
|
|
//$this->_arrayOfColumnData[$name] = $column; |
|
|
|
|
42
|
|
|
$foreignKey = new ForeignKey(array($column), array($refColumn)); |
43
|
|
|
$column->foreignKey = $foreignKey; |
44
|
|
|
//$this->table->addForeignKey($foreignKey); |
|
|
|
|
45
|
|
|
$column->setFlag(Column::AUTO_ID, false); |
46
|
|
|
} else { |
47
|
|
|
$column->propertyName = $name; |
48
|
|
|
$column->schemaName = Utils::fromCamelCase($name); |
49
|
|
|
$column->table = $this->table; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
if ($column->flags & Column::AUTO_ID) { |
54
|
|
|
$this->table->autoIdColumn = $column; |
55
|
|
|
if (!$this->table->primaryKey) { |
|
|
|
|
56
|
|
|
$this->table->setPrimaryKey($column); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($column->isUnique) { |
61
|
|
|
$index = new Index($column); |
62
|
|
|
$index->setType(Index::TYPE_UNIQUE); |
63
|
|
|
$this->table->addIndex($index); |
64
|
|
|
} elseif ($column->isIndexed) { |
65
|
|
|
$index = new Index($column); |
66
|
|
|
$index->setType(Index::TYPE_KEY); |
67
|
|
|
$this->table->addIndex($index); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->table->database()->getUtility()->checkColumn($column); |
71
|
|
|
$this->_arrayOfColumnData[$name] = $column; |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function __get($name) |
76
|
|
|
{ |
77
|
|
|
if (!isset($this->_arrayOfColumnData[$name])) { |
78
|
|
|
throw new Exception('Unknown column ' . $name); |
79
|
|
|
} |
80
|
|
|
return $this->_arrayOfColumnData[$name]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function __isset($name) |
84
|
|
|
{ |
85
|
|
|
return isset($this->_arrayOfColumnData[$name]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function __unset($name) |
89
|
|
|
{ |
90
|
|
|
if (!isset($this->_arrayOfColumnData[$name])) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
unset($this->_arrayOfColumnData[$name]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Column[] |
98
|
|
|
*/ |
99
|
|
|
public function getArray() |
100
|
|
|
{ |
101
|
|
|
return $this->_arrayOfColumnData; |
102
|
|
|
} |
103
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.