/** * get all articles assigned to given $category * * @param ArticleCategory $category * @return Array */ public static function getArticlesForCategory(ArticleCategory $category) { $articles = array(); $rows = Application::getInstance()->getDb()->doPreparedQuery('SELECT * FROM articles WHERE articlecategoriesID = ?', array($category->getId())); foreach ($rows as $r) { if (!isset(self::$instancesById[$r['articlesID']])) { // create Article instance if it does not yet exist $article = self::createInstance($r); self::$instancesByAlias[$article->alias] = $article; self::$instancesById[$article->id] = $article; } $articles[] = self::$instancesById[$r['articlesID']]; } return $articles; }
/** * returns array of ArticleCategory objects identified by numeric id or alias * * @param array $ids contains mixed category ids or alias * @throws ArticleCategoryException * @return array */ public static function getInstances(array $ids) { $db = Application::getInstance()->getDb(); $toRetrieveById = array(); $toRetrieveByAlias = array(); foreach ($ids as $id) { if (is_numeric($id)) { $id = (int) $id; if (!isset(self::$instancesById[$id])) { $toRetrieveById[] = $id; } } else { if (!isset(self::$instancesByAlias[$id])) { $toRetrieveByAlias[] = $id; } } $where = array(); if (count($toRetrieveById)) { $where[] = 'c.articlecategoriesID IN (' . implode(',', array_fill(0, count($toRetrieveById), '?')) . ')'; } if (count($toRetrieveByAlias)) { $where[] = 'c.alias IN (' . implode(',', array_fill(0, count($toRetrieveByAlias), '?')) . ')'; } if (count($where)) { $rows = $db->doPreparedQuery(' SELECT c.*, p.articlecategoriesID AS parentID FROM articlecategories c LEFT JOIN articlecategories p ON p.l < c.l AND p.r > c.r AND p.level = c.level - 1 WHERE ' . implode(' OR ', $where), array_merge($toRetrieveById, $toRetrieveByAlias)); foreach ($rows as $row) { if (!empty($row['level'])) { if (empty($row['parentID'])) { throw new ArticleCategoryException("Category '{$row['Title']}' not properly nested.", ArticleCategoryException::ARTICLECATEGORY_NOT_NESTED); } else { $cat = new self($row['Title'], ArticleCategory::getInstance($row['parentID'])); } } else { $cat = new self($row['Title']); } $cat->id = $row['articlecategoriesID']; $cat->alias = $row['Alias']; $cat->r = $row['r']; $cat->l = $row['l']; $cat->level = $row['level']; $cat->customSort = $row['customSort']; self::$instancesByAlias[$cat->alias] = $cat; self::$instancesById[$cat->id] = $cat; } } } $categories = array(); foreach ($ids as $id) { $categories[] = self::getInstance($id); } return $categories; }
/** * add WHERE clause that filters for $category * * @param ArticleCategory $category * * @return \vxPHP\Orm\Custom\ArticleQuery */ public function filterByCategory(ArticleCategory $category) { $this->addCondition("a.articlecategoriesID = ?", $category->getId()); return $this; }