Entity 클래스
데이터를 단순 배열이 아닌 객체로 다루며, 커스텀 메서드를 추가합니다.
Entity 클래스 구조
// app/Entities/Post.php
namespace App\Entities;
use CodeIgniter\Entity\Entity;
class Post extends Entity
{
// 타입 자동 캐스팅
protected $casts = [
'id' => 'integer',
'views' => 'integer',
];
// 커스텀 메서드 - 본문 요약
public function getExcerpt(int $length = 100): string
{
return mb_strlen($this->content) > $length
? mb_substr($this->content, 0, $length) . '...'
: $this->content;
}
// 커스텀 메서드 - 날짜 포맷
public function getFormattedDate(): string
{
return $this->created_at?->format('Y-m-d H:i') ?? '';
}
}