3
Let’s assume that in a system for a bus company I have entities: Line, car and travel.
Each one 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 his 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 very 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
- Voyage 1
- Journey 2
- Car 002
- Voyage 1
- Journey 2
- Car 001
- Line 456...
What would be the best way to do that?
I’m sorry if the explanation was long, I’m a beginner and I don’t know if my solution is too obvious.
Codes have been simplified to shorten the question
Hi Lucas, you still need help in this matter?
– Fernando Mertins
Hello Fernando, yes I do. Thank you.
– Lucas Henrique