Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace GK\Collections;
- use GK\Container;
- use GK\Entities\Product;
- use GK\entities\Size;
- class ProductCollection extends \ArrayObject
- {
- /**
- * @param null $limit
- * @param null $offset
- * @return self
- */
- public static function fetchAll($limit=null, $offset=null) {
- if ($limit !== null && !is_numeric($limit)) {
- $limit = NULL;
- }
- if ($offset !==NULL && !is_numeric($offset)) {
- $offset = NULL;
- }
- $Select = Container::getInst()->qbf->newSelect()
- ->cols([
- 'id','brand','name','url','price_usd','description',
- 'picture1','picture2','picture3','picture4','picture5',
- ])
- ->from('products p');
- if ($limit) {
- $Select->limit($limit);
- }
- if ($offset) {
- $Select->offset($offset);
- }
- $products = Container::getInst()->db->fetchAll($Select);
- $count = sizeof($products);
- $Collection = new self($count);
- $Select = Container::getInst()->qbf->newSelect()
- ->cols(['product_id','size_eur','size_us','size_ru',])
- ->from('sizes');
- //$st = Container::getInst()->db->perform($Select);
- $sizes = Container::getInst()->db->perform($Select)
- ->fetchAll(
- \PDO::FETCH_CLASS | \PDO::FETCH_GROUP,
- Size::class
- );
- for ($i=0;$i<$count;++$i) {
- $P = new Product();
- $P->setAttributes($products[$i]);
- if (isset($sizes[$P->id])){
- $P->setSizes(SizeCollection::fromArray($sizes[$P->id]));
- }
- $Collection->offsetSet($i,$P);
- }
- return $Collection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment