-
Notifications
You must be signed in to change notification settings - Fork 0
/
GActiveRecord.php
148 lines (131 loc) · 5.11 KB
/
GActiveRecord.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
class GActiveRecord extends GModel {
protected $_db;
public $pk = 'id';
public $name = 'nome';
public $scenario = 'insert';
public $formOptions = array();
public $isNewRecord;
const REL_BELONGS_TO = 'belongs_to';
const REL_MANY_MANY = 'many_many';
public function __construct() {
$this->_db = GPDO::instance();
parent::__construct();
if (method_exists($this, 'formOptions')) $this->formOptions = $this->formOptions();
}
public function getAttributeNames() {
return $this->_db->columns($this->tableName());
}
public function form($type = '') {
$form = new GForm();
$form->model = $this;
$form->options = $this->formOptions;
return $form->form($type, $this->getAttributesForm());
}
public function validate() {
return true;
}
public function getAttributesForm() {
return '*';
}
/*
Retorna o id em caso de insert e true em caso de update
*/
public function save($validate = TRUE, $attributes = null) {
if ($validate && !$this->validate()) return false;
if ($this->id) {
$sql = "UPDATE {$this->tableName()} SET {$this->_sqlFields('update')} WHERE {$this->pk} = {$this->id}";
if ($this->_db->exec($sql)) return true;
return false;
} else {
$sql = "INSERT INTO {$this->tableName()} {$this->_sqlFields('insert')}";
if ($this->_db->exec($sql)) {
$this->id = $this->_db->id();
return $this->id;
}
return false;
}
}
protected function _sqlFields($type = 'insert') {
if ($type == 'insert') {
$fields = array();
$values = array();
foreach ($this->attributeNames as $name) {
$fields[] = $name;
$values[] = $this->$name;
}
return '(' . implode(',', $fields) . ") VALUES ('" . implode("','", $values) . "')";
} else {
foreach ($this->attributeNames as $name) {
$fields[] = $name . "='" . $this->$name . "'";
}
return implode(',', $fields);
}
}
protected function _sqlGetFields($attributes = array()) {
if (!$attributes) return '*';
return implode(',', $attributes);
}
protected function _sqlAttributes($attributes) {
$result = array();
$this->_log(var_export($attributes, true));
foreach ($attributes as $field=>$value) {
// Não monta com atributos não existentes
if (!in_array($field, $this->getAttributeNames())) continue;
$result[] = $field . "='" . $value . "'";
}
$this->_log(var_export($result, true));
return implode(' AND ', $result);
}
protected function _log($message) {
// desativado
return;
$content = file_get_contents('gmanagerlog');
$content .= "\n" . date('H:i:s d/m/Y') . ' - ' . $message;
file_put_contents('gmanagerlog', $content);
}
protected function _find($attributes = null, $condition = '', $params = array(), $type = '', $sqlForce = null) {
$sql = "SELECT * FROM {$this->tableName()}";
if (is_numeric($condition)) $sql .= " WHERE {$this->pk} = $condition";
elseif ($attributes) $sql .= " WHERE " . $this->_sqlAttributes($attributes);
elseif ($condition) $sql .= " WHERE $condition";
if ($sqlForce) $sql = $sqlForce;
$class = get_class($this);
$this->_log("SQL $class: $sql");
$result = $this->_db->query($sql);
if ($result) {
$list = array();
foreach ($result as $item) {
$obj = new $class();
$obj->attributes = $item;
// Preenche as relações
foreach ($this->relations as $attribute=>$relation) {
// Por enquanto suporte somente para REL_BELONGS_TO. TO_DO: Implementar REL_MANY_MANY
$rObjName = $relation[1];
$rObj = new $rObjName;
$attr = $relation[0];
$rObj = $rObj->find($obj->$attr);
if ($rObj != null)
$obj->$attribute = $rObj;
}
if (!$type) return $obj;
$list[] = $obj;
}
if (!$list) return null;
return $list;
}
return null;
}
public function find($condition = '', $params = array()) {
return $this->_find(null, $condition, $params, '');
}
public function findAll($condition = '', $params = array(), $sqlForce = null) {
return $this->_find(null, $condition, $params, 'all', $sqlForce);
}
public function findByAttributes($attributes, $condition = '', $params = array()) {
return $this->_find($attributes, $condition, $params, '');
}
public function findAllByAttributes($attributes, $condition = '', $params = array()) {
return $this->_find($attributes, $condition, $params, 'all');
}
}