CListView shows more data than expected

Good morning, I have a problem with the CListView component, MySQL shows the correct amount of expected rows in the query, however once I run it into yii, despite debugger shows the right query, CListView shows one more record, I’ve attached some images about it, showing the SQL query I got from debugger, the one I copied and pasted in MySQL workbench, showing the expected results, and the results gotten from CListView, also added the Model, View and Controller Related Code used for this component.

Any help would be greatly appreciated, thank you

Model - Usuario





public function obtenerListaAmigos($id){

		

		$pk = $id;

		$lista_amigos = new CDbCriteria;

		$lista_amigos->select = 'nombre, apellido1, apellido2';

		$lista_amigos->distinct = true;

		$lista_amigos->join = 'INNER JOIN tbl_amigo_por_usuario ON fkUsuario = :pk';

		$lista_amigos->condition = 'pkUsuario != :pk';

		$lista_amigos->params = array(':pk'=>$pk);

		

		$this->findAll($lista_amigos);

		

}




Model - AmigoPorUsuario





public function obtenerCantidadAmigos($id){

	

		$cuenta = new CDbCriteria;

		$cuenta->select = 'COUNT(*) as total';

		$cuenta->condition = 'fkUsuario = :id';

		$cuenta->params = array(':id'=>$id);

		

		

		$this->find($cuenta);

		

}




Controller





public function actionLista(){

	

		$id =  new Usuario;

		$cuenta = new AmigoPorUsuario;

		

		$id = Usuario::model()->obtenerListaAmigos(Yii::app()->user->getState('__id'));

		$cuenta = $cuenta->obtenerCantidadAmigos(Yii::app()->user->getState('__id'));

		

		$datos =  new CActiveDataProvider('Usuario', array(

								   'totalItemCount'=>$cuenta,

					  			   'pagination'=>array(

								                      'pageSize'=>5,

										      ),

								   )

						  );		

		

		$this->render('lista', array('origen_datos'=>$datos));

		

}




View





<?php


$this->pageTitle=Yii::app()->name . ' - Lista';

$this->breadcrumbs=array('Lista',);


?>


<h1>Lista</h1>

<p>Amigos actualmente agregados</p>


<?php

/*$this->widget('zii.widgets.grid.CGridView', array(

 'dataProvider'=>$dataProvider,

 ));*/

?>


<?php 


$this->widget('zii.widgets.CListView', array(

		                             'dataProvider'=>$origen_datos,

					     'itemView'=>'_listar',

					     )

	     ); 


?>




Hello!

Well in fact, you are filtering something in your controller, but at the end, you don’t use it at all :)

Your ListView widget is using a data provider that has no filtering at all, only item count, which you don’t really need after all.

Instead of filtering at model level, you should use a CDbCriteria, apply the filters to it, and attach it to the data provider.

See the standard code generated by Gii, it gives you a good starting point.

Thank you for your reply, however I think I’m missing something. I looked into yii generated views for the same table, they don’t seem to be filtering at all or I don’t really get how does it do it. Sorry I have no experience with yii at all, this is my first project using it and still sometimes I feel a bit lost.

The above query is meant to filter any undesired data, I’ve tested it on MySQL and it works well, however once CListView runs it, it shows an inexistent result (assuming it’s running the query as it does) can you give me any input about what would I need to filter, and how to do it?

Thank you very much in advance!

By the way, I used the same code, but on controller approach, returning the desired data, is this approach ok? why is this code working on controller but not in model, am I missing something? thank you and sorry for keep asking and bump, I’m new on yii and I’d like to learn as much as possible since I love this framework