Skip to content

Instantly share code, notes, and snippets.

@lolychank
Last active October 6, 2019 14:48
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 lolychank/90f222e62985d012213175fce89ae209 to your computer and use it in GitHub Desktop.
Save lolychank/90f222e62985d012213175fce89ae209 to your computer and use it in GitHub Desktop.
<?php
class TaskRepository
{
// __construct()
public function getById(Id $id): TaskView
{
$sql = '
SELECT
t.id,
t.name,
t.project_id AS project,
GROUP_CONCAT(sbt.name) AS subTasks,
t.date
FROM
tasks t
LEFT JOIN
sub_tasks sbt
WHERE
id = :id
';
$stmt = $this->connection->prepare($sql);
$stmt->execute(['id' => $id->getValue()]);
$stmt->setFetchMode(\PDO::FETCH_CLASS, TaskView::class);
$task = $stmt->fetch();
if (!$task) {
throw new EntityNotFoundException();
}
return $task;
}
}
<?php
class TaskView
{
private $id;
private $name;
private $project;
/**
* @var array
*/
private $subTasks;
/**
* @var \DateTimeImmutable
*/
private $date;
public function getId(): string
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function getProjectId(): string
{
return $this->project;
}
public function getSubTasks(): array
{
return \explode(',', $this->subTasks);
}
public function getDate(): \DateTimeImmutable
{
return \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $this->date);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment