Skip to content

Instantly share code, notes, and snippets.

@franzose
Created April 7, 2021 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franzose/1d5e02b681a28234a57a01e5f5a8e847 to your computer and use it in GitHub Desktop.
Save franzose/1d5e02b681a28234a57a01e5f5a8e847 to your computer and use it in GitHub Desktop.
Query objects
<?php
class SomeEntityRepository extends EntityRepository
{
public function findThis()
{
// method body
}
public function findThat()
{
// method body
}
public function findThese()
{
// method body
}
public function findThose()
{
// method body
}
}
<?php
class FindThisQuery
{
private $em;
private $repository;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->repository = $em->getRepository(Entity::class);
}
public function __invoke()
{
// query logic
}
}
class FindThatQuery
{
private $em;
private $repository;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->repository = $em->getRepository(Entity::class);
}
public function __invoke()
{
// query logic
}
}
class FindTheseQuery
{
private $em;
private $repository;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->repository = $em->getRepository(Entity::class);
}
public function __invoke()
{
// query logic
}
}
class FindThoseQuery
{
private $em;
private $repository;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
$this->repository = $em->getRepository(Entity::class);
}
public function __invoke()
{
// query logic
}
}
<?php
class Foo
{
private $findThisQuery;
private $findThatQuery;
public function __construct(FindThisQuery $findThisQuery, FindThatQuery $findThatQuery)
{
$this->findThisQuery = $findThisQuery;
$this->findThatQuery = $findThatQuery;
}
pubic function doStuff()
{
$resultForThis = ($this->findThisQuery)();
$resultForThat = ($this->findThatQuery)();
// do something...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment