1: <?php
2:
3: 4: 5: 6: 7:
8:
9:
10:
11: 12: 13: 14: 15: 16: 17: 18:
19: class NConventionalReflection extends NObject implements 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: public function setConnection(NConnection $connection)
72: {}
73:
74:
75: protected function getColumnFromTable($name)
76: {
77: if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
78: return $match[1];
79: }
80:
81: return $name;
82: }
83:
84: }
85: