| @@ 249-282 (lines=34) @@ | ||
| 246 | * |
|
| 247 | * @return int|array count of objects |
|
| 248 | */ |
|
| 249 | public function getCount(\CriteriaElement $criteria = null) |
|
| 250 | { |
|
| 251 | $field = ''; |
|
| 252 | $groupby = false; |
|
| 253 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
| 254 | if ('' != $criteria->groupby) { |
|
| 255 | $groupby = true; |
|
| 256 | $field = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used |
|
| 257 | } |
|
| 258 | } |
|
| 259 | $sql = 'SELECT ' . $field . 'COUNT(*) FROM ' . $this->table; |
|
| 260 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
| 261 | $sql .= ' ' . $criteria->renderWhere(); |
|
| 262 | if ('' != $criteria->groupby) { |
|
| 263 | $sql .= $criteria->getGroupby(); |
|
| 264 | } |
|
| 265 | } |
|
| 266 | $result = $this->db->query($sql); |
|
| 267 | if (!$result) { |
|
| 268 | return 0; |
|
| 269 | } |
|
| 270 | if (false === $groupby) { |
|
| 271 | list($count) = $this->db->fetchRow($result); |
|
| 272 | ||
| 273 | return $count; |
|
| 274 | } else { |
|
| 275 | $ret = []; |
|
| 276 | while (list($id, $count) = $this->db->fetchRow($result)) { |
|
| 277 | $ret[$id] = $count; |
|
| 278 | } |
|
| 279 | ||
| 280 | return $ret; |
|
| 281 | } |
|
| 282 | } |
|
| 283 | ||
| 284 | /** |
|
| 285 | * delete an object from the database by id. |
|
| @@ 627-660 (lines=34) @@ | ||
| 624 | * |
|
| 625 | * @return array|string |
|
| 626 | */ |
|
| 627 | public function getSum(\CriteriaElement $criteria = null, $sum = '*') |
|
| 628 | { |
|
| 629 | $field = ''; |
|
| 630 | $groupby = false; |
|
| 631 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
| 632 | if ('' != $criteria->groupby) { |
|
| 633 | $groupby = true; |
|
| 634 | $field = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used |
|
| 635 | } |
|
| 636 | } |
|
| 637 | $sql = 'SELECT ' . $field . "SUM($sum) FROM " . $this->table; |
|
| 638 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
| 639 | $sql .= ' ' . $criteria->renderWhere(); |
|
| 640 | if ('' != $criteria->groupby) { |
|
| 641 | $sql .= $criteria->getGroupby(); |
|
| 642 | } |
|
| 643 | } |
|
| 644 | $result = $this->db->query($sql); |
|
| 645 | if (!$result) { |
|
| 646 | return 0; |
|
| 647 | } |
|
| 648 | if (false === $groupby) { |
|
| 649 | list($sum) = $this->db->fetchRow($result); |
|
| 650 | ||
| 651 | return $sum; |
|
| 652 | } else { |
|
| 653 | $ret = []; |
|
| 654 | while (list($id, $sum) = $this->db->fetchRow($result)) { |
|
| 655 | $ret[$id] = $sum; |
|
| 656 | } |
|
| 657 | ||
| 658 | return $ret; |
|
| 659 | } |
|
| 660 | } |
|
| 661 | ||
| 662 | /** |
|
| 663 | * @param null|CriteriaElement $criteria |
|
| @@ 668-701 (lines=34) @@ | ||
| 665 | * |
|
| 666 | * @return array|string |
|
| 667 | */ |
|
| 668 | public function getMax(\CriteriaElement $criteria = null, $max = '*') |
|
| 669 | { |
|
| 670 | $field = ''; |
|
| 671 | $groupby = false; |
|
| 672 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
| 673 | if ('' != $criteria->groupby) { |
|
| 674 | $groupby = true; |
|
| 675 | $field = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used |
|
| 676 | } |
|
| 677 | } |
|
| 678 | $sql = 'SELECT ' . $field . "MAX($max) FROM " . $this->table; |
|
| 679 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
|
| 680 | $sql .= ' ' . $criteria->renderWhere(); |
|
| 681 | if ('' != $criteria->groupby) { |
|
| 682 | $sql .= $criteria->getGroupby(); |
|
| 683 | } |
|
| 684 | } |
|
| 685 | $result = $this->db->query($sql); |
|
| 686 | if (!$result) { |
|
| 687 | return 0; |
|
| 688 | } |
|
| 689 | if (false === $groupby) { |
|
| 690 | list($max) = $this->db->fetchRow($result); |
|
| 691 | ||
| 692 | return $max; |
|
| 693 | } else { |
|
| 694 | $ret = []; |
|
| 695 | while (list($id, $max) = $this->db->fetchRow($result)) { |
|
| 696 | $ret[$id] = $max; |
|
| 697 | } |
|
| 698 | ||
| 699 | return $ret; |
|
| 700 | } |
|
| 701 | } |
|
| 702 | ||
| 703 | /** |
|
| 704 | * @param null $criteria |
|