Access to select count attribute

Hi, what is the best way to do this?

I have this query:


$criteria = new CDbCriteria();

$criteria->select = 'COUNT(t.codigoProducto) as productos';

$criteria->with = [

'relacion1',

'relacion2',

];

$productos = Productos::model()->findAll($criteria);

In a foreach, how do I access to "productos" of the SELECT COUNT?

Thanks




// in your Productos model declare a public property like so

public $productTotal;


$criteria         = new CDbCriteria();

// make sure you count the products as productTotal 

$criteria->select = 'COUNT(t.codigoProducto) as productTotal';

$criteria->with   = [

    'relacion1',

    'relacion2'

];

$productos = Productos::model()->findAll($criteria);




// then you should be able to access it like so

echo $product->productTotal;

I did not got why you people consider $data = Model::model()->count(); ??

and pass criteria to count() if required and can user $data->relation1->field;

Let me know if required more explanation.

This is the best way? I ask because a public property is not pretty in the OOP. :rolleyes:

I’d go for Model::model()->with(array(‘relation1’, ‘relation2’))->count()

@Yuv I need to loop through records…

Exactly!