Я часто сталкиваюсь с кодом, подобным следующему (ссылка Slim tutorial на github ).
TicketMapper.php
class TicketMapper extends Mapper
{
public function getTickets() {
$sql = "SELECT t.id, t.title, t.description, c.component
from tickets t
join components c on (c.id = t.component_id)";
$stmt = $this->db->query($sql);
$results = [];
while($row = $stmt->fetch()) {
$results[] = new TicketEntity($row);
}
return $results;
}
/**
* Get one ticket by its ID
*
* @param int $ticket_id The ID of the ticket
* @return TicketEntity The ticket
*/
public function getTicketById($ticket_id) {
$sql = "SELECT t.id, t.title, t.description, c.component
from tickets t
join components c on (c.id = t.component_id)
where t.id = :ticket_id";
$stmt = $this->db->prepare($sql);
$result = $stmt->execute(["ticket_id" => $ticket_id]);
if($result) {
return new TicketEntity($stmt->fetch());
}
}
public function save(TicketEntity $ticket) {
$sql = "insert into tickets
(title, description, component_id) values
(:title, :description,
(select id from components where component = :component))";
$stmt = $this->db->prepare($sql);
$result = $stmt->execute([
"title" => $ticket->getTitle(),
"description" => $ticket->getDescription(),
"component" => $ticket->getComponent(),
]);
if(!$result) {
throw new Exception("could not save record");
}
}
}
TicketEntity.php
class TicketEntity
{
protected $id;
protected $title;
protected $description;
protected $component;
/**
* Accept an array of data matching properties of this class
* and create the class
*
* @param array $data The data to use to create
*/
public function __construct(array $data) {
// no id if we're creating
if(isset($data['id'])) {
$this->id = $data['id'];
}
$this->title = $data['title'];
$this->description = $data['description'];
$this->component = $data['component'];
}
public function getId() {
return $this->id;
}
public function getTitle() {
return $this->title;
}
public function getDescription() {
return $this->description;
}
public function getShortDescription() {
return substr($this->description, 0, 20);
}
public function getComponent() {
return $this->component;
}
}
Моя текущая практика не использует классы сущностей, и мои методы отображения просто возвращают класс stdClass, как показано ниже:
class TicketMapper extends Mapper
{
public function getTickets() {
$sql = "...";
$stmt = $this->db->query($sql);
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
public function getTicketById($ticket_id) {
$sql = "...";
$stmt = $this->db->prepare($sql);
$result = $stmt->execute(["ticket_id" => $ticket_id]);
return $stmt->fetch(); //Assuming my PDO is configured to return an object only
}
public function save($ticket) {/* no change */}
}
Почему результаты базы данных часто заключаются вкакой-то класс сущности?Есть ли критерии, по которым можно было бы определить, стоит ли это делать?