public function getCategory($category_id) { $db = Database::getDB(); $query = "SELECT * FROM categories\n WHERE categoryID = '{$category_id}'"; $statement = $db->query($query); $row = $statement->fetch(); $category = new Category(); $category->setID($row['categoryID']); $category->setName($row['categoryName']); return $category; }
/** * @param string $value */ private function _setValue($value) { if ($this->_field == 'categories') { if (!is_object($value)) { $catID = $value; $value = new Category(); $value->setID($catID); } elseif (!$value instanceof Category) { throw new InvalidArgumentException('Category values must be passed as string IDs or ' . __NAMESPACE__ . '\\Category instances.'); } API::validateCategory($value); $this->_value = $value; } else { $this->_value = self::$_validator->string($value, 'Values used in filters must be passed as strings.'); } }
public function getProducts() { $db = Database::getDB(); $query = 'SELECT * FROM products INNER JOIN categories ON products.categoryID = categories.categoryID'; $result = $db->query($query); $products = array(); foreach ($result as $row) { // create the Category object $category = new Category(); $category->setID($row['categoryID']); $category->setName($row['categoryName']); // create the Product object $product = new Product(); $product->setCategory($category); $product->setId($row['productID']); $product->setName($row['productName']); $product->setDescription($row['productDescription']); $product->setPrice($row['listPrice']); $products[] = $product; } return $products; }
public function fetchAllCategories() { $this->db->select('id,name')->order_by("id", "asc"); $query = $this->db->get('website_forum_categories'); $result = array(); if ($query->rowCount() > 1) { $rows = $this->db->fetchAll($query); do { $row = $rows->current(); $category = new Category(); $category->setName($row->name)->setID($row->id); $result[] = $category; } while ($rows->next()); } else { if ($query->rowCount() == 1) { $row = $this->db->fetch($query); $category = new Category(); $category->setName($row->name)->setID($row->id); $result[] = $category; } else { $category = new Category(); $category->setName("error1"); $category->setID(0); $result[] = $category; } } return $result; }
/** * Validates that the given Google\MyBusiness\Category instance contains * either a name or ID that exists in the available category data. * * @param Google\MyBusiness\Category $category */ public static function validateCategory(Category $category) { if (!self::$_staticPropsReady) { self::_initStaticProperties(); } // Refresh category data if necessary $lastFetchData = self::_getLastFetchData('google_business_categories'); if (!$lastFetchData || (int) $lastFetchData['fetch_date'] < time() - GOOGLE_MYBUSINESS_API_CATEGORIES_CACHE_DURATION) { self::_storeFetchData('google_business_categories', self::_refreshCategories()); } $catID = $category->getID(); $catName = $category->getName(); if (!$catID && !$catName) { throw new InvalidArgumentException('Cannot validate a category unless it has an ID and/or a name.'); } try { // Always prefer to validate on the basis of the ID, if present if ($catID) { $stmt = self::$_DB_STATEMENTS['google_business_categories']['select_by_id']; $stmt->bindValue(':id', $catID, \PDO::PARAM_STR); } else { $stmt = self::$_DB_STATEMENTS['google_business_categories']['select_by_name']; $stmt->bindValue(':name', $catName, \PDO::PARAM_STR); } $stmt->execute(); $stmt->bindColumn('id', $dbID, \PDO::PARAM_STR); $stmt->bindColumn('name', $dbName, \PDO::PARAM_STR); if (!$stmt->fetch(\PDO::FETCH_BOUND)) { if ($catID) { $message = 'The category ID "' . $catID . '" is invalid.'; } else { $message = 'The category name "' . $catName . '" is invalid.'; } throw new UnexpectedValueException($message); } if ($catID) { $category->setName($dbName); } else { $category->setID($dbID); } } catch (\PDOException $e) { throw new RuntimeException('Caught database error while validating category.', null, $e); } }