Last active
December 15, 2021 07:55
-
-
Save amberlex78/b518737b376074dc704644c10f43b64d to your computer and use it in GitHub Desktop.
Symfony 5.4: Get all posts ids by slug of tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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