riverplate riverplate - 1 year ago 49
PHP Question

PHP - Can't print my sql result

I'm using Yii2 framework.

I'm trying to get the number of records of my table and if the result is smaller than X number, show this number of records.

When I run my script, the error is:

'Object of class app\models\BusquedaCotizacion could not be converted
to string'

This is my code:

$query = BusquedaCotizacion::find()
->where('LIKE', cotizacion, 'BOB')
$bobresult = count($query);
if ($bobresult < 6) {
echo ("No se obtuvieron todas las consultas de Bolivia. De las 6 posibles, se obtuvieron $bobresult.");
echo ("Se obtuvieron todas las cotizaciones de Bolivia");

I would appreciate any kind of help.

Answer Source

The $query result that you are trying to count with count($query) is a collection of models and not an array (or string) yuo are doing in the wrong wya

You should use this way for count and models

$query = BusquedaCotizacion::find()
                            ->where('LIKE', cotizacion, 'BOB');

$numRow = $query->count():

$models = query->all();

In $query you should prepare the Sql (PDO based) You need and on this you can apply the function you need ... count() for getting the count(*) and all() fpr getting al the models related;