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