1

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()
                                ->select('cotizacion')
                                ->where('LIKE', cotizacion, 'BOB')
                                ->all();
            $bobresult = count($query);
            if ($bobresult < 6) {
                 echo ("No se obtuvieron todas las consultas de Bolivia. De las 6 posibles, se obtuvieron $bobresult.");
             }else{
                 echo ("Se obtuvieron todas las cotizaciones de Bolivia");
             }

I would appreciate any kind of help.

4
  • 1
    This is because you are echo'ing the $bobresult variable inside the quoted string. Commented Aug 2, 2016 at 19:10
  • @Bizley The use of a $var inside double quote is normal in PHP .. in this way the $vars is evaluated and the content i showed properly in the echo ... the error of th OP came from others Commented Aug 2, 2016 at 19:28
  • @scaisEdge I know it's normal. Also I know that not every class has its __toString() implementation and this one doesn't. But you are right, the error probably comes from different place. Commented Aug 2, 2016 at 20:39
  • @Bizley in this case the user fails to calculate the result rather than a string, and obtains a collection of objects Commented Aug 2, 2016 at 20:42

1 Answer 1

1

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()
                            ->select('cotizacion')
                            ->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;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.