/** * Load prices for bundle and return as an associative array * * @param BundleProxy $bundle * * @return array */ public function getPrices(BundleProxy $bundle) { $result = $this->_queryBuilderFactory->getQueryBuilder()->select($this->_columns)->from(self::TABLE_NAME)->where('bundle_id = ?i', [$bundle->getID()])->getQuery()->run(); $prices = []; foreach ($result as $row) { $prices[$row->currency] = $row->price; } return $prices; }
/** * Load product data and create instances of ProductRow to assign to bundle * * @param BundleProxy $bundle * * @return array */ public function getProductRows(BundleProxy $bundle) { $result = $this->_queryBuilderFactory->getQueryBuilder()->select($this->_columns)->from('p', self::PRODUCT_TABLE)->leftJoin('o', 'p.product_row_id = o.product_row_id', self::OPTION_TABLE)->where('p.bundle_id = ?i', [$bundle->getID()])->orderBy('p.product_row_id ASC')->getQuery()->run(); $productRowData = []; $productRows = []; // Reorganise data into mutlidimensional array split into product rows to allow for multiple options per row foreach ($result as $row) { if (!array_key_exists($row->id, $productRowData)) { $productRowData[$row->id] = ['product_id' => $row->product_id, 'options' => $this->_getRowOptionsArray($row->option_name, $row->option_value), 'quantity' => $row->quantity]; } $productRowData[$row->id]['options'] = $productRowData[$row->id]['options'] + $this->_getRowOptionsArray($row->option_name, $row->option_value); } foreach ($productRowData as $id => $data) { $productRow = new ProductRow($data['product_id'], $data['options'], $data['quantity']); $productRow->setID($id); $productRows[] = $productRow; } return $productRows; }
/** * Run query and use result data to build an instance of BundleProxy * * @param bool $returnAsArray Will return an array of Bundles if set to true, will return the first value * of the array if false * @return array | BundleProxy */ private function _load($returnAsArray = true) { if (null === $this->_queryBuilder) { throw new \LogicException('No query builder set!'); } $result = $this->_queryBuilder->getQuery()->run(); $bundles = []; foreach ($result as $row) { $bundle = new BundleProxy($this->_loaders, $this->_defaultCurrency); $bundle->setID((int) $row->id); $bundle->setName($row->name); $bundle->setAllowCodes((bool) $row->allowCodes); if ($row->start) { $bundle->setStart(new DateTimeImmutable(date('c', $row->start))); } if ($row->end) { $bundle->setEnd(new DateTimeImmutable(date('c', $row->end))); } if ($row->imageID) { $bundle->setImageID($row->imageID); } $bundle->getAuthorship()->create(new DateTimeImmutable(date('c', $row->createdAt)), $this->_userLoader->getByID($row->createdBy)); if ($row->updatedAt) { $bundle->getAuthorship()->update(new DateTimeImmutable(date('c', $row->updatedAt)), $this->_userLoader->getByID($row->updatedBy)); } if ($row->deletedAt) { $bundle->getAuthorship()->delete(new DateTimeImmutable(date('c', $row->deletedAt)), $this->_userLoader->getByID($row->deletedAt)); } $bundles[$bundle->getID()] = $bundle; } return $returnAsArray ? $bundles : array_shift($bundles); }
/** * Lazy load image assigned to bundle using the FileLoader * * @param BundleProxy $bundle * * @return array|\Message\Mothership\FileManager\File\File */ public function getImage(BundleProxy $bundle) { return $this->_fileLoader->getByID($bundle->getImageID()); }