Skip to content

Commit d2aa513

Browse files
committed
API change
1 parent 73bd522 commit d2aa513

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

dbObject.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __get ($name) {
2222
$modelName = $this->relations[$name][1];
2323
switch ($relationType) {
2424
case 'hasone':
25-
return $modelName::byId($this->data[$name]);
25+
return $modelName::ObjectBuilder()->byId($this->data[$name]);
2626
break;
2727
case 'hasmany':
2828
$key = $this->relations[$name][2];
@@ -44,7 +44,7 @@ public function __get ($name) {
4444
}
4545

4646
public function __isset ($name) {
47-
if ($this->data[$name])
47+
if (isset ($this->data[$name]))
4848
return isset ($this->data[$name]);
4949

5050
if (property_exists ($this->db, $name))
@@ -108,22 +108,21 @@ public function remove () {
108108
}
109109

110110

111-
public static function byId ($id, $fields = null) {
112-
return static::getOne ($fields, $id);
111+
private function byId ($id, $fields = null) {
112+
return $this->getOne ($fields, $id);
113113
}
114114

115-
public static function getOne ($fields = null, $primaryKey = null, $obj = null) {
116-
$obj = new static;
115+
private function getOne ($fields = null, $primaryKey = null) {
117116
if ($primaryKey)
118-
$obj->db->where ($obj->primaryKey, $primaryKey);
117+
$this->db->where ($this->primaryKey, $primaryKey);
119118

120-
$results = $obj->db->getOne ($obj->dbTable, $fields);
121-
if (isset($obj->jsonFields) && is_array($obj->jsonFields)) {
122-
foreach ($obj->jsonFields as $key)
119+
$results = $this->db->getOne ($this->dbTable, $fields);
120+
if (isset($this->jsonFields) && is_array($this->jsonFields)) {
121+
foreach ($this->jsonFields as $key)
123122
$results[$key] = json_decode ($results[$key]);
124123
}
125-
if (isset($obj->arrayFields) && is_array($obj->arrayFields)) {
126-
foreach ($obj->arrayFields as $key)
124+
if (isset($this->arrayFields) && is_array($this->arrayFields)) {
125+
foreach ($this->arrayFields as $key)
127126
$results[$key] = explode ("|", $results[$key]);
128127
}
129128
if (static::$returnType == 'Array')
@@ -135,17 +134,16 @@ public static function getOne ($fields = null, $primaryKey = null, $obj = null)
135134
return $item;
136135
}
137136

138-
public static function get ($limit = null, $fields = null) {
139-
$obj = new static;
137+
private function get ($limit = null, $fields = null) {
140138
$objects = Array ();
141-
$results = $obj->db->get($obj->dbTable, $limit, $fields);
139+
$results = $this->db->get ($this->dbTable, $limit, $fields);
142140
foreach ($results as &$r) {
143-
if (isset ($obj->jsonFields) && is_array($obj->jsonFields)) {
144-
foreach ($obj->jsonFields as $key)
141+
if (isset ($this->jsonFields) && is_array($this->jsonFields)) {
142+
foreach ($this->jsonFields as $key)
145143
$r[$key] = json_decode ($r[$key]);
146144
}
147-
if (isset ($obj->arrayFields) && is_array($obj->arrayFields)) {
148-
foreach ($obj->arrayFields as $key)
145+
if (isset ($this->arrayFields) && is_array($this->arrayFields)) {
146+
foreach ($this->arrayFields as $key)
149147
$r[$key] = explode ("|", $r[$key]);
150148
}
151149
if (static::$returnType == 'Object') {
@@ -173,19 +171,22 @@ public function count () {
173171
return $res['cnt'];
174172
}
175173

176-
177174
public function __call ($method, $arg) {
175+
if (method_exists ($this, $method))
176+
return call_user_func_array (array ($this, $method), $arg);
177+
178178
call_user_func_array (array ($this->db, $method), $arg);
179179
return $this;
180180
}
181181

182182
public static function __callStatic ($method, $arg) {
183183
$obj = new static;
184-
call_user_func_array (array ($obj, $method), $arg);
184+
$result = call_user_func_array (array ($obj, $method), $arg);
185+
if (method_exists ($obj, $method))
186+
return $result;
185187
return $obj;
186188
}
187189

188-
189190
public function toJson () {
190191
return json_encode ($this->data);
191192
}

dbObject/test.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
$dept->authcode = Array('1234','123456');
1515
$dept->iscallerid = 1;
1616
$dept->insert();
17+
echo "ID = " . $dept->id . "\n";
1718

1819
$dept2 = new department([
1920
'userid' => '11',
@@ -25,29 +26,36 @@
2526
$dept2->iscallerid=1;
2627
print_r ($dept2->data);
2728
$dept2->save();
29+
echo "department is of class " . get_class ($dept2) . "\n";
2830

2931
echo "List\n";
3032
$depts = department::get ();
3133
foreach ($depts as $d) {
3234
// print_r ($d->data);
3335
echo $d . "\n";
36+
echo "department is of class " . get_class ($d) . "\n";
3437
}
3538

3639
echo "getOne\n";
37-
$dept3 = department::byId ("181");
40+
$dept3 = department::byId ($dept->id);
3841
echo 'cnt ' . $dept3->count . "\n";
39-
$dept3->authcode=333;
42+
$dept3->authcode=443;
4043
$dept3->save();
4144
print_r ($dept3->data) . "\n";
42-
45+
echo "department is of class " . get_class ($dept3) . "\n";
4346

4447
echo "hasOne\n";
4548
echo json_encode ($dept3->userid->data);
49+
echo "user is of class " . get_class ($dept3->userid) . "\n";
4650

4751
echo "\nhasMany\n";
4852
foreach ($dept3->userid->departments as $d) {
49-
echo $d;
53+
echo $d;
54+
echo "department is of class " . get_class ($d) . "\n";
5055
}
5156

57+
$user = user::byId (41);
58+
echo "user is of class " . get_class ($user) . "\n";
59+
5260

5361
?>

0 commit comments

Comments
 (0)