Commit c71ea51b authored by Fedrico Otaran's avatar Fedrico Otaran

Se hace el fetch automático mapeando los atributos necesarios

parent 948fd7f2
......@@ -6,11 +6,23 @@ class User
private $first_name;
private $last_name;
public function __construct($id, $first_name, $last_name)
{
$this->id = $id;
$this->first_name = $first_name;
$this->last_name = $last_name;
public function __construct() { }
/**
* Este método se utiliza para mapear cada atributo en el fetch
*/
function __set($name, $value) {
switch ($name) {
case "id":
$this->id = $value;
break;
case "nombre":
$this->first_name = $value;
break;
case "apellido":
$this->last_name = $value;
break;
}
}
public function getId()
......
......@@ -4,12 +4,7 @@ class UserRepository extends BaseRepository
{
public function all()
{
$data = $this->execute("select * from usuarios where estado = ?;", ['Confirmado']);
$users = [];
foreach ($data->fetchAll() as $user) {
$users[] = new User($user['id'], $user['nombre'], $user['apellido']);
}
return $users;
$stmt = $this->execute("select * from usuarios where estado = ?;", ['Confirmado']);
return $stmt->fetchAll(PDO::FETCH_CLASS, "User");
}
}
......@@ -11,11 +11,24 @@
{% block content %}
<h1>Usuarios</h1>
<ul>
<table>
<thead>
<tr>
<th>#</th>
<th>Nombre</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<li><a href="{{ user.id }}">{{ user.fullName }}</a></li>
<tr>
<td>{{ user.id }}</td>
<td><a href="{{ user.id }}">{{ user.fullName }}</a></td>
<td><a href="{{ user.id }}">Eliminar</a></td>
</tr>
{% endfor %}
</ul>
<tbody>
</table>
<p><a href="/"><- Volver<a></p>
{% endblock %}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment