Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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