Skip to content

Instantly share code, notes, and snippets.

@amberlex78
Last active December 15, 2021 07:55
Show Gist options
  • Save amberlex78/b518737b376074dc704644c10f43b64d to your computer and use it in GitHub Desktop.
Save amberlex78/b518737b376074dc704644c10f43b64d to your computer and use it in GitHub Desktop.
Symfony 5.4: Get all posts ids by slug of tag
<?php
namespace App\ReadModel\Blog;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
class PostFetcher
{
public function __construct(
private Connection $connection
) {
}
/**
* @throws Exception
*/
public function findAllActiveByTag(string $slug): array
{
return $this->connection->createQueryBuilder()
->select('bp.id')
->from('blog_posts', 'bp')
->leftJoin('bp', 'blog_post_tag', 'bpt', 'bp.id = bpt.post_id')
->leftJoin('bpt', 'blog_tags', 'bt', 'bt.id = bpt.tag_id')
->where('bt.slug = :slug')
->setParameter('slug', $slug)
->executeQuery()
->fetchFirstColumn();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment