55use ArrayAccess ;
66use ArrayIterator ;
77use IteratorAggregate ;
8- use function MongoDB \BSON \toJSON ;
98use 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
1716class 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