Let's assume that in a system for a bus company I have entities: Line, car and travel. Each has three classes:
The entity itself,
class Linha extends Model
{
protected $id;
// outras propriedades.
public function getId() {
return $this->id;
}
// outros métodos.
Your collection,
class LinhaCollection extends ArrayObject {}
And your mapper.
class LinhaMapper extends Mapper
{
public function findAll() {
$sql = "SELECT * FROM 'linhas'";
return $this->fetchCollection($sql); // Retorna uma coleção de objetos do tipo Linha.
}
So listing all the lines is pretty simple:
$lm = new LinhaMapper;
$linhas = $lm->findAll();
foreach ($linhas as $linha) {
echo $linha->getNome();
}
/*
* Linha 123
* Linha 456
* Linha 159
*/
I want to list in my View all lines, cars and trips as follows (tree type):
- Line 123
- Car 001
- Travel 1
- Travel 2
- Car 002
- Travel 1
- Travel 2
- Car 001
- Line 456 ...
What would be the best way to do this?
Sorry if the explanation was long, I'm a beginner and I do not know if my solution is very obvious. The codes have been simplified to shorten the question