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