Skip to content

Commit f6f9c2a

Browse files
committed
重构 更清晰的 Database 层
1 parent e87fdd3 commit f6f9c2a

File tree

8 files changed

+458
-292
lines changed

8 files changed

+458
-292
lines changed

.idea/workspace.xml

Lines changed: 123 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

App/Controller/apiController.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?php
22
namespace Controller;
3-
use Model\Demo;
43
use System\Collection;
4+
use Model\Demo;
5+
use System\Database;
56

67
class apiController {
78
/**
89
* @param $id
910
*/
1011
public function test($id){
1112
// echo $_SERVER['REQUEST_METHOD'];
12-
$test = Demo::where(['title' => '还是标题'])->orWhereRaw('1=1');
13+
$test = Database::model(Demo::class)->where()->paginate(3);
1314
print_r($test);
1415
}
1516

@@ -23,15 +24,15 @@ public function find($id){
2324

2425
public function where(){
2526
// 条件检索数据表(条件数组)
26-
$conditionArray = ["id" => "6"];
27-
$resultObjectArray = Demo::where($conditionArray);
27+
$conditionArray = ["author" => "Rytia"];
28+
$resultObjectArray = Demo::where($conditionArray)->fetchAll();
2829
print_r($resultObjectArray);
2930
}
3031

3132
public function whereRaw(){
3233
// 条件检索数据表(SQL语句)
3334
$conditionStatement = 'author LIKE "Rytia"';
34-
$resultObjectArray = Demo::whereRaw($conditionStatement);
35+
$resultObjectArray = Demo::whereRaw($conditionStatement)->fetchAll();
3536
print_r($resultObjectArray);
3637
}
3738

@@ -44,20 +45,20 @@ public function all(){
4445
public function raw(){
4546
// 执行SQL语句
4647
$sqlStatement = 'SELECT * FROM {table} WHERE author LIKE "Rytia"';
47-
$resultObjectArray = Demo::raw($sqlStatement);
48+
$resultObjectArray = Demo::raw($sqlStatement)->fetchAll();
4849
print_r($resultObjectArray);
4950
}
5051

5152
public function search(){
5253
// 数据表字段搜索
53-
$resultObjectArray = Demo::search("title", "%测试%");
54+
$resultObjectArray = Demo::search("title", "%%");
5455
print_r($resultObjectArray);
5556
}
5657

57-
public function orWhere($id){
58+
public function orWhere(){
5859
// 多重条件检索数据表(条件数组)
5960
// 支持 where、whereRaw、orWhere、orWhereRaw
60-
$test = Demo::where(['title' => '还是标题'])->orWhere(['title' => '测试标题']);
61+
$test = Demo::where(['title' => '还是标题'])->orWhere(['title' => '测试标题'])->fetchAll();
6162
print_r($test);
6263
}
6364

App/System/Collection.php

Lines changed: 34 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,25 @@
55
use ArrayAccess;
66
use ArrayIterator;
77
use IteratorAggregate;
8-
use function MongoDB\BSON\toJSON;
98
use System\Interfaces\Jsonable;
109

1110
/**
12-
* PHP-Quick-ORM 框架的数据集合类
11+
* PHP-QuickORM 框架的数据集合类
1312
* @author Rytia <rytia@outlook.com>
1413
* @copyright 2018 PHP-JSORM
1514
*/
1615

1716
class Collection implements ArrayAccess, Countable, IteratorAggregate
1817
{
19-
protected $objectSQL;
18+
2019
protected $collectionItems = [];
2120

2221
// 为 Collection 分页方法提供支持
2322
public $collectionPages;
2423

2524
// 新建 Collection 方法
26-
27-
public function __construct($collectionItems = [], $sqlStatement = '') {
25+
public function __construct($collectionItems = []) {
2826
$this->collectionItems = $collectionItems;
29-
$this->objectSQL = $sqlStatement;
3027
}
3128

3229
/**
@@ -263,6 +260,25 @@ public function sum($field = null) {
263260

264261
// ORM 实用方法
265262

263+
/**
264+
* 将集合中的关联数组转换为实例数组
265+
* @param model $modelClass, string $sqlStatementCache
266+
* @return Collection
267+
* @uses 在新建或调用多条记录时可通过此类创建实例数组
268+
*/
269+
public function format($modelClass, $sqlStatementCache = '') {
270+
$objectArray = [];
271+
// 构建实例数组
272+
foreach($this->collectionItems as $key => $value) {
273+
$model = new $modelClass($value,$sqlStatementCache);
274+
if (!is_null($model)) {
275+
array_push($objectArray,$model);
276+
}
277+
}
278+
return new Collection($objectArray,$sqlStatementCache);
279+
}
280+
281+
266282
/**
267283
* 依据字段排序元素
268284
* @param string $field, string $orderBy
@@ -303,24 +319,22 @@ public function sortBy($field = null, $method = "ASC"){
303319
public function paginate($pageNum, $furtherPageInfo = true){
304320
// 保存集合总大小
305321
$total = $this->count();
306-
307322
// 获取当前页码
308323
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
309324
$startAt = (($currentPage-1)*$pageNum);
325+
// 使用 PHP 原生切片
326+
$this->collectionItems = array_slice($this->collectionItems,$startAt,$pageNum);
327+
// 调用分页构造方法
328+
return $this->forPage($pageNum, $currentPage, $total, $furtherPageInfo);
329+
}
310330

311-
if($this->collectionItems[0] instanceof Model) {
312-
// 拼接 SQL 语句:select * from table limit start,pageNum
313-
$sql = $this->objectSQL." LIMIT ".$startAt.",".$pageNum;;
314-
$db = new Database();
315-
$db->prepare($sql);
316-
317-
// 修改 Collection 数据内容
318-
$this->collectionItems = $db->fetchAll();
319-
$this->objectSQL = $sql;
320-
} else {
321-
// 使用 PHP 原生切片
322-
$this->collectionItems = array_slice($this->collectionItems,$startAt,$pageNum);
323-
}
331+
/**
332+
* 集合分页构造
333+
* @param int $pageNum, int $currentPage, int $total, boolean $furtherPageInfo
334+
* @return Collection
335+
* @uses Collection 分页功能
336+
*/
337+
public function forPage($pageNum, $currentPage, $total, $furtherPageInfo = true){
324338

325339
if($furtherPageInfo){
326340
// 添加分页的属性
@@ -336,103 +350,6 @@ public function paginate($pageNum, $furtherPageInfo = true){
336350
}
337351

338352

339-
// TODO: ORM 数据库查询方法
340-
341-
342-
public function select(){
343-
344-
}
345-
346-
public function where(){
347-
// 判断 $sqlConditionArray 是否传入:加入空条件的判断使开发变得简便
348-
if(empty($sqlConditionArray)){
349-
// 未传入条件,SQL语句不做任何改动
350-
} else {
351-
// 传入条件,进行 SQL 语句拼接
352-
foreach ($sqlConditionArray as $key => $value) {
353-
if (isset($sql)) {
354-
$sql .= " AND ".$key.'="'.$value.'"';
355-
} else {
356-
$sql = $key.'="'.$value.'"';
357-
}
358-
}
359-
$this->objectSQL = $this->objectSQL.' AND ('.$sql.')';
360-
}
361-
362-
$db = new Database();
363-
$db->prepare($this->objectSQL);
364-
$this->collectionItems = $db->fetchAll();
365-
return $this;
366-
}
367-
368-
public function whereRaw(){
369-
// 判断 $sqlConditionArray 是否传入:加入空条件的判断使开发变得简便
370-
if(empty($sqlConditionStatement)){
371-
// 未传入条件,SQL语句不做任何改动
372-
} else {
373-
// 传入条件,进行 SQL 语句拼接
374-
$this->objectSQL = $this->objectSQL.' AND ('.$sqlConditionStatement.')';
375-
}
376-
377-
$db = new Database();
378-
$db->prepare($this->objectSQL);
379-
$this->collectionItems = $db->fetchAll();
380-
return $this;
381-
}
382-
383-
/**
384-
* 通过数组条件检索数据表
385-
* @param array $sqlConditionArray
386-
* @return Collection
387-
*/
388-
public function orWhere($sqlConditionArray){
389-
// 判断 $sqlConditionArray 是否传入:加入空条件的判断使开发变得简便
390-
if(empty($sqlConditionArray)){
391-
// 未传入条件,SQL语句不做任何改动
392-
} else {
393-
// 传入条件,进行 SQL 语句拼接
394-
foreach ($sqlConditionArray as $key => $value) {
395-
if (isset($sql)) {
396-
$sql .= " AND ".$key.'="'.$value.'"';
397-
} else {
398-
$sql = $key.'="'.$value.'"';
399-
}
400-
}
401-
$this->objectSQL = $this->objectSQL.' OR ('.$sql.')';
402-
}
403-
404-
$db = new Database();
405-
$db->prepare($this->objectSQL);
406-
$this->collectionItems = $db->fetchAll();
407-
return $this;
408-
}
409-
410-
/**
411-
* 通过 SQL 语句条件检索数据表
412-
* @param string $sqlConditionStatement
413-
* @return Collection
414-
*/
415-
public function orWhereRaw($sqlConditionStatement){
416-
// 判断 $sqlConditionArray 是否传入:加入空条件的判断使开发变得简便
417-
if(empty($sqlConditionStatement)){
418-
// 未传入条件,SQL语句不做任何改动
419-
} else {
420-
// 传入条件,进行 SQL 语句拼接
421-
$this->objectSQL = $this->objectSQL.' OR ('.$sqlConditionStatement.')';
422-
}
423-
424-
$db = new Database();
425-
$db->prepare($this->objectSQL);
426-
$this->collectionItems = $db->fetchAll();
427-
return $this;
428-
}
429-
430-
431-
public function update(){
432-
433-
}
434-
435-
436353
// PHP ArrayAccess 接口支持
437354

438355
/**

0 commit comments

Comments
 (0)