1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Database\Reflection;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22: 23: 24:
25: class ConventionalReflection extends Nette\Object implements Nette\Database\IReflection
26: {
27:
28: protected $primary;
29:
30:
31: protected $foreign;
32:
33:
34: protected $table;
35:
36:
37:
38: 39: 40: 41: 42: 43:
44: public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
45: {
46: $this->primary = $primary;
47: $this->foreign = $foreign;
48: $this->table = $table;
49: }
50:
51:
52:
53: public function getPrimary($table)
54: {
55: return sprintf($this->primary, $this->getColumnFromTable($table));
56: }
57:
58:
59:
60: public function getHasManyReference($table, $key)
61: {
62: $table = $this->getColumnFromTable($table);
63: return array(
64: sprintf($this->table, $key, $table),
65: sprintf($this->foreign, $table, $key),
66: );
67: }
68:
69:
70:
71: public function getBelongsToReference($table, $key)
72: {
73: $table = $this->getColumnFromTable($table);
74: return array(
75: sprintf($this->table, $key, $table),
76: sprintf($this->foreign, $key, $table),
77: );
78: }
79:
80:
81:
82:
83: public function setConnection(Nette\Database\Connection $connection)
84: {}
85:
86:
87:
88: protected function getColumnFromTable($name)
89: {
90: if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '$)', $name, $match)) {
91: return $match[1];
92: }
93:
94: return $name;
95: }
96:
97: }
98: