grikdotnet

collection sample

Apr 3rd, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2. namespace GK\Collections;
  3.  
  4.  
  5. use GK\Container;
  6. use GK\Entities\Product;
  7. use GK\entities\Size;
  8.  
  9. class ProductCollection extends \ArrayObject
  10. {
  11.  
  12.     /**
  13.      * @param null $limit
  14.      * @param null $offset
  15.      * @return self
  16.      */
  17.     public static function fetchAll($limit=null, $offset=null) {
  18.         if  ($limit !== null && !is_numeric($limit)) {
  19.             $limit = NULL;
  20.         }
  21.         if  ($offset !==NULL && !is_numeric($offset)) {
  22.             $offset = NULL;
  23.         }
  24.         $Select = Container::getInst()->qbf->newSelect()
  25.             ->cols([
  26.                 'id','brand','name','url','price_usd','description',
  27.                 'picture1','picture2','picture3','picture4','picture5',
  28.             ])
  29.             ->from('products p');
  30.         if ($limit) {
  31.             $Select->limit($limit);
  32.         }
  33.         if ($offset) {
  34.             $Select->offset($offset);
  35.         }
  36.         $products = Container::getInst()->db->fetchAll($Select);
  37.         $count = sizeof($products);
  38.         $Collection = new self($count);
  39.  
  40.         $Select = Container::getInst()->qbf->newSelect()
  41.             ->cols(['product_id','size_eur','size_us','size_ru',])
  42.             ->from('sizes');
  43.         //$st = Container::getInst()->db->perform($Select);
  44.         $sizes = Container::getInst()->db->perform($Select)
  45.             ->fetchAll(
  46.                 \PDO::FETCH_CLASS | \PDO::FETCH_GROUP,
  47.                 Size::class
  48.             );
  49.  
  50.         for ($i=0;$i<$count;++$i) {
  51.             $P = new Product();
  52.             $P->setAttributes($products[$i]);
  53.             if (isset($sizes[$P->id])){
  54.                 $P->setSizes(SizeCollection::fromArray($sizes[$P->id]));
  55.             }
  56.             $Collection->offsetSet($i,$P);
  57.         }
  58.         return $Collection;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment