nicoorfi
8/21/2019 - 12:46 PM

Query builder PDO fetch


    /**
     * Get article ids by category
     *
     * @param Category $category
     *
     * @return array
     */
    public function getArticleIdsByCategory(Category $category)
    {
        $query = $this->database->createQueryBuilder();
        $blockedCats = implode(',', $this->getExcludedCategoryIds());
        $query->select('articles.id')
            ->from('s_articles', 'articles')
            ->innerJoin('articles', 's_articles_categories_ro', 'ac', 'ac.articleId = articles.id')
            ->where('ac.categoryID = :catId')
            ->setParameter(':catId', $category->getId())
            ->groupBy('articles.id')
            ->orderBy('articles.id', 'ASC');
        if (!empty($blockedCats)) {
            $subSelect = $this->database->createQueryBuilder()
                ->select('articleID')
                ->from('s_articles_categories_ro')
                // Instead of setting the parameter in the main query below, it could be quoted here:
                // ->where('status = ' . $connection->quote(UserSurveyStatus::ACCESSED))
                ->where('categoryID IN (:categoryIds)');
            $query->andWhere('articles.id NOT IN (' . $subSelect . ')')
                ->setParameter('categoryIds', $blockedCats);
        }
        $statement = $query->execute();
        $selected  = $statement->fetchAll(\PDO::FETCH_COLUMN);
        return $selected;
    }