问题
I made a custom query in my cakephp 3.0, but when I wanna display the data in a table, the rows are empty. This is my function in controller:
public function estadisticas()
{
$conn = ConnectionManager::get('default');
$stmt = $conn->execute('SELECT c.nombre, c.idComplejo, t.canchaFK, count(*) AS cantidadTurnos FROM turno t inner join complejo c inner join ciudad cc inner join cancha ca inner join usuario u on t.canchaFK=ca.idCancha and c.idComplejo=ca.complejoFK and u.idUsuario=t.usuarioFK WHERE u.esComplejo=0 and cc.paisFK="1" and u.paisFK="1" and cc.idCiudad=c.ciudadFK and noAsistio=0 and fecha between "2016-07-01" and "2016-07-30" group by canchaFK');
$turnostotales = $stmt ->fetchAll('assoc');
$this->set('turnostotales',$turnostotales);
}
The query works perfect. Also, The page show me the table with 19 empty row (19 row is the result of the query).
This is the part of code where I display the data:
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th><?= $this->Paginator->sort('idComplejo', 'ID Complejo') ?></th>
<th><?= $this->Paginator->sort('canchaFK', '# Cancha') ?></th>
<th><?= $this->Paginator->sort('cantidadTurnos', 'Cantidad Turnos') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($turnostotales as $turnostotale): ?>
<tr>
<td><?= h($turnostotale->idComplejo) ?></td>
<td><?= h($turnostotale->canchaFK) ?></td>
<td><?= h($turnostotale->cantidadTurnos) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Thanks for helping me!
回答1:
Fixed query format for anyone else reading:
SELECT c.nombre,
c.idcomplejo,
t.canchafk,
Count(*) AS cantidadturnos
FROM turno t
INNER JOIN complejo c
INNER JOIN ciudad cc
INNER JOIN cancha ca
INNER JOIN usuario u
ON t.canchafk=ca.idcancha
AND c.idcomplejo=ca.complejofk
AND u.idusuario=t.usuariofk
where u.escomplejo=0
AND cc.paisfk="1"
AND u.paisfk="1"
AND cc.idciudad=c.ciudadfk
AND noasistio=0
AND fecha BETWEEN "2016-07-01" AND "2016-07-30"
GROUP BY canchafk
Looks like your query is broken. Try it in heidi/sql program of choice and fix it there.
Hows this:
SELECT c.nombre,
c.idcomplejo,
t.canchafk,
Count(*) AS cantidadturnos
FROM turno t
INNER JOIN usuario u ON u.idusuario = t.usuariofk
INNER JOIN cancha ca ON t.canchafk = ca.idcancha
INNER JOIN complejo c ON c.idcomplejo = ca.complejofk
INNER JOIN ciudad cc ON cc.idciudad=c.ciudadfk
WHERE u.escomplejo=0
AND cc.paisfk="1"
AND u.paisfk="1"
AND noasistio=0
AND fecha BETWEEN "2016-07-01" AND "2016-07-30"
GROUP BY canchafk
It looks like lots of your joins don't have ON clauses.
来源:https://stackoverflow.com/questions/38819007/cakephp-3-x-show-me-empty-row-with-my-custom-query