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: class ConventionalReflection extends Nette\Object implements Nette\Database\IReflection
25: {
26:
27: protected $primary;
28:
29:
30: protected $foreign;
31:
32:
33: protected $table;
34:
35:
36: 37: 38: 39: 40: 41:
42: public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
43: {
44: $this->primary = $primary;
45: $this->foreign = $foreign;
46: $this->table = $table;
47: }
48:
49:
50: public function getPrimary($table)
51: {
52: return sprintf($this->primary, $this->getColumnFromTable($table));
53: }
54:
55:
56: public function getHasManyReference($table, $key)
57: {
58: $table = $this->getColumnFromTable($table);
59: return array(
60: sprintf($this->table, $key, $table),
61: sprintf($this->foreign, $table, $key),
62: );
63: }
64:
65:
66: public function getBelongsToReference($table, $key)
67: {
68: $table = $this->getColumnFromTable($table);
69: return array(
70: sprintf($this->table, $key, $table),
71: sprintf($this->foreign, $key, $table),
72: );
73: }
74:
75:
76: public function setConnection(Nette\Database\Connection $connection)
77: {}
78:
79:
80: protected function getColumnFromTable($name)
81: {
82: if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
83: return $match[1];
84: }
85:
86: return $name;
87: }
88:
89: }
90: