Skip to content

Commit 5350b8d

Browse files
committed
Bugfixes and API change
1 parent d2aa513 commit 5350b8d

File tree

5 files changed

+220
-116
lines changed

5 files changed

+220
-116
lines changed

dbObject.php

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
2-
abstract class dbObject {
2+
class dbObject {
33
private $db;
44
public $data;
55
public $isNew = true;
6-
public static $returnType = 'Object';
6+
public $returnType = 'Object';
7+
private $_with = Array();
78

89
public function __construct ($data = null) {
910
$this->db = MysqliDb::getInstance();
@@ -16,31 +17,33 @@ public function __set ($name, $value) {
1617
}
1718

1819
public function __get ($name) {
19-
if (property_exists ($this, 'relations')) {
20-
if (isset ($this->relations[$name])) {
21-
$relationType = strtolower ($this->relations[$name][0]);
22-
$modelName = $this->relations[$name][1];
23-
switch ($relationType) {
24-
case 'hasone':
25-
return $modelName::ObjectBuilder()->byId($this->data[$name]);
26-
break;
27-
case 'hasmany':
28-
$key = $this->relations[$name][2];
29-
return $modelName::ObjectBuilder()->where($key, $this->data[$this->primaryKey])->get();
30-
break;
31-
default:
32-
break;
33-
}
34-
}
35-
}
36-
37-
38-
if (isset ($this->data[$name]))
39-
return $this->data[$name];
40-
41-
if (property_exists ($this->db, $name))
42-
return $this->db->$name;
20+
if (!property_exists ($this, 'relations')) {
21+
if (isset ($this->data[$name]))
22+
return $this->data[$name];
4323

24+
if (property_exists ($this->db, $name))
25+
return $this->db->$name;
26+
}
27+
if (!isset ($this->relations[$name]))
28+
return;
29+
30+
$relationType = strtolower ($this->relations[$name][0]);
31+
$modelName = $this->relations[$name][1];
32+
switch ($relationType) {
33+
case 'hasone':
34+
$obj = new $modelName;
35+
$obj->returnType = $this->returnType;
36+
return $obj->byId($this->data[$name]);
37+
break;
38+
case 'hasmany':
39+
$key = $this->relations[$name][2];
40+
$obj = new $modelName;
41+
$obj->returnType = $this->returnType;
42+
return $obj->where($key, $this->data[$this->primaryKey])->get();
43+
break;
44+
default:
45+
break;
46+
}
4447
}
4548

4649
public function __isset ($name) {
@@ -112,9 +115,16 @@ private function byId ($id, $fields = null) {
112115
return $this->getOne ($fields, $id);
113116
}
114117

118+
private function processWith (&$data) {
119+
if (count ($this->_with) == 0)
120+
return;
121+
foreach ($this->_with as $w)
122+
$data[$w] = $this->$w;
123+
}
124+
115125
private function getOne ($fields = null, $primaryKey = null) {
116126
if ($primaryKey)
117-
$this->db->where ($this->primaryKey, $primaryKey);
127+
$this->db->where ($this->dbTable . '.' . $this->primaryKey, $primaryKey);
118128

119129
$results = $this->db->getOne ($this->dbTable, $fields);
120130
if (isset($this->jsonFields) && is_array($this->jsonFields)) {
@@ -125,8 +135,10 @@ private function getOne ($fields = null, $primaryKey = null) {
125135
foreach ($this->arrayFields as $key)
126136
$results[$key] = explode ("|", $results[$key]);
127137
}
128-
if (static::$returnType == 'Array')
138+
if ($this->returnType == 'Array') {
139+
$this->processWith ($results);
129140
return $results;
141+
}
130142

131143
$item = new static ($results);
132144
$item->isNew = false;
@@ -146,18 +158,25 @@ private function get ($limit = null, $fields = null) {
146158
foreach ($this->arrayFields as $key)
147159
$r[$key] = explode ("|", $r[$key]);
148160
}
149-
if (static::$returnType == 'Object') {
161+
if ($this->returnType == 'Object') {
150162
$item = new static ($r);
151163
$item->isNew = false;
152164
$objects[] = $item;
153-
}
165+
} else
166+
$this->processWith($r);
154167
}
155-
if (static::$returnType == 'Object')
168+
if ($this->returnType == 'Object')
156169
return $objects;
170+
157171
return $results;
158172
}
159173

160-
public function join ($objectName, $key = null, $joinType = 'LEFT') {
174+
private function with ($objectName) {
175+
$this->_with[] = $objectName;
176+
177+
return $this;
178+
}
179+
private function join ($objectName, $key = null, $joinType = 'LEFT') {
161180
$joinObj = new $objectName;
162181
if (!$key)
163182
$key = $objectName . "id";
@@ -212,11 +231,12 @@ private function prepareData () {
212231
continue;
213232
}
214233

215-
if (in_array ($key, $this->jsonFields))
234+
if (isset ($this->jsonFields) && in_array ($key, $this->jsonFields))
216235
$sqlData[$key] = json_encode($value);
217-
else
236+
else if (isset ($this->arrayFields) && in_array ($key, $this->arrayFields))
218237
$sqlData[$key] = implode ("|", $value);
219-
238+
else
239+
$sqlData[$key] = $value;
220240
}
221241
return $sqlData;
222242
}

dbObject/models/accData.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

dbObject/models/department.php renamed to dbObject/models/product.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,18 @@
1010
* @property string authcode
1111
* @property string iscallerid
1212
*/
13-
class department extends dbObject {
14-
protected $dbTable = "departments";
13+
class product extends dbObject {
14+
protected $dbTable = "products";
1515
protected $primaryKey = "id";
1616
protected $dbFields = Array (
17-
'userid' => 'int:required',
18-
'name' => 'int:required',
19-
'authcode' => 'int',
20-
'userid' => 'int',
21-
'iscallerid' => 'int',
22-
'testvar' => 'int'
17+
'userId' => 'int:required',
18+
'customerId' => 'int:required',
19+
'productName' => 'char:required'
2320
);
2421
protected $relations = Array (
25-
'userid' => Array ("hasOne", "user")
22+
'userId' => Array ("hasOne", "user")
2623
);
2724

28-
protected $jsonFields = Array ('authcode');
29-
3025
public function last () {
3126
$this->where ("id" , 130, '>');
3227
return $this;

0 commit comments

Comments
 (0)