I am basically trying to bring the last 5 records from a table.
The only catch is that I don’t want some records with the same x_id to be brought since I would be repeating the x_id, so I pass a ‘group’ condition to achieve this.
However, when adding this new group condition, something odd happens and the result for the query is randomly omitting some rows of the table. So, randomly, some records are brought, and some are not. So some record which should have been brought is not.
For instance:
LAST 10 records in my table are: 1-2-3-4-5-6-7-8-9-0
After doing my query, I should have as a result: 0-9-8-7-6
However, at random, I get: 9-8-6-5-3 OR 0-8-7-6-4
Basically it skips some records the shit =//
$condicionesComentarios = array('conditions' => array('Comentario.aceptado' => 1), 'limit' => 5, 'order' => 'Comentario.id DESC', 'group' => array('Comentario.empresa_id'));
$ultimosComentarios = $this->Empresa->Comentario->find('all', $condicionesComentarios);
UPDATE FOR CLARIFICATION
Empresa hasMany Comentario
Small portion of Comentarios table:
**id** **empresa_id**
1 7
2 9
3 3
4 1
5 1
6 4
7 8
8 5
EXPECTED RESULT: last 5 records from the table, without repeated empresa_id
query result-> (id = 8, 7, 6, 5, 3) *Note that row with id: 4 should be skipped, to avoid empresa_id repetition*
WHAT I GET: Some of the records are randomly skipped when they should not.
query result-> (id = 7, 6, 5, 2, 1) Note that here row with id: 8 should not be skipped, but it’s nonetheless.
Joni you were almost right, put me on track. Your code perse was not working, though a small modification made it work properly:
As you can see I had to modify the ‘order’ and add an extra condition to ‘fields’.
Again what it does: brings the last 5 rows from Comentarios table, but without repeating the empresa_id field from within those last rows.
Thanks for the help 😉