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