1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan\Orm;
12:
13: 14: 15: 16: 17: 18: 19:
20: class Field extends \Kotchasan\Database\Db
21: {
22: 23: 24: 25: 26:
27: public $table_alias;
28: 29: 30: 31: 32:
33: public $table_name;
34: 35: 36: 37: 38:
39: protected $conn = 'mysql';
40: 41: 42: 43: 44:
45: protected $exists;
46: 47: 48: 49: 50:
51: protected $primaryKey = 'id';
52: 53: 54: 55: 56:
57: protected $table;
58:
59: 60: 61: 62: 63:
64: public function __construct($param = null)
65: {
66: if (!empty($param)) {
67: foreach ($param as $key => $value) {
68: $this->{$key} = $value;
69: }
70: $this->exists = true;
71: } else {
72: $this->exists = false;
73: }
74: }
75:
76: 77: 78: 79: 80:
81: public static function create()
82: {
83: $obj = new static();
84: return $obj;
85: }
86:
87: 88: 89:
90: public function delete()
91: {
92: $rs = new Recordset(get_called_class());
93: return $rs->delete(array($this->primaryKey, (int) $this->{$this->primaryKey}), 1);
94: }
95:
96: 97: 98: 99: 100:
101: public function getConn()
102: {
103: return $this->conn;
104: }
105:
106: 107: 108: 109: 110: 111: 112: 113:
114: public function getFullTableName($table)
115: {
116: $dbname = empty($this->db->settings->dbname) ? '' : '`'.$this->db->settings->dbname.'`.';
117: $prefix = empty($this->db->settings->prefix) ? '' : $this->db->settings->prefix.'_';
118: return $dbname.'`'.$prefix.(isset($this->db->tables->$table) ? $this->db->tables->$table : $table).'`';
119: }
120:
121: 122: 123: 124: 125:
126: public function getPrimarykey()
127: {
128: return $this->primaryKey;
129: }
130:
131: 132: 133: 134: 135:
136: public function getTableName()
137: {
138: return $this->table_name;
139: }
140:
141: 142: 143: 144: 145: 146: 147:
148: public function getTableWithAlias($alias = null)
149: {
150: return $this->table_name.' AS '.(empty($alias) ? $this->table_alias : $alias);
151: }
152:
153: 154: 155: 156: 157:
158: public function initTableName($db)
159: {
160: $this->db = $db;
161: if (empty($this->table)) {
162: $class = get_called_class();
163: if (preg_match('/[a-z0-9]+\\\\([a-z0-9_]+)\\\\Model/i', $class, $match)) {
164: $t = strtolower($match[1]);
165: } elseif (preg_match('/Models\\\\([a-z0-9_]+)/i', $class, $match)) {
166: $t = strtolower($match[1]);
167: } else {
168: $t = strtolower($class);
169: }
170: $this->table_name = $this->getFullTableName($t);
171: $this->table_alias = $t;
172: } elseif (preg_match('/^([a-z0-9A-Z_]+)(\s+(as|AS))?\s+([a-zA-Z0-9]{1,})$/', $this->table, $match)) {
173: $this->table_name = $this->getFullTableName($match[1]);
174: $this->table_alias = strlen($match[4]) < 3 ? $match[4] : '`'.$match[4].'`';
175: } else {
176: $this->table_name = $this->getFullTableName($this->table);
177: $this->table_alias = '`'.$this->table.'`';
178: }
179: }
180:
181: 182: 183:
184: public function save()
185: {
186: $rs = new Recordset(get_called_class());
187: if ($this->exists) {
188: $rs->update(array($this->primaryKey, (int) $this->{$this->primaryKey}), $this);
189: } else {
190: $rs->insert($this);
191: }
192: }
193: }
194: