findAll()

I’m attempting to do a simple AR findAll() like it’s done in the Phonebook demo:


return Contact::model()->findAll();

which returns:


Array

(

    [0] => stdClass Object

        (

            [id] => 1

            [name] => James Blake

            [phone] => 123-123-1234

        )


    [1] => stdClass Object

        (

            [id] => 3

            [name] => Thomas Maiden

            [phone] => 567-567-5678

        )


)

but with this:


$ftypes = Foodtypes::model()->findAll();

instead of getting the 5 records that are in the table, I’m getting an object of schema info on the table.

I’m a newbie to Yii (2 days), so hoping for someone to read this who experienced a similar result?

Thanks in advance, I’m dumbfounded ???

UPDATE: I attempted to perform the same query on a table with quite a few more records (~1000):


Restaurants::model()->findAll()

, and although I could understand if it took a little while, it actually became unresponsive, more than once. Sounds like a configuration/environment issue maybe? I’m working using the latest XAMPP, under Windows 7, fyi.

And not sure why this didn’t post to the Bug Discussion section, I meant to, my apologies.

UPDATE: Here’s my trace, the SQL statement looks just fine


15:45:28.794713	trace	system.db.CDbCommand	

Querying SQL: SHOW COLUMNS FROM `tbl_foodtypes`

in C:\xampp\htdocs\emenu.com\api\protected\models\Foodtypes.php (19)

in

C:\xampp\htdocs\emenu.com\api\protected\modules\ajax\controllers\MenuController.php

(24)

in C:\xampp\htdocs\emenu.com\api\index.php (13)

15:45:28.800051	trace	system.db.CDbCommand	

Querying SQL: SHOW CREATE TABLE `tbl_foodtypes`

in C:\xampp\htdocs\emenu.com\api\protected\models\Foodtypes.php (19)

in

C:\xampp\htdocs\emenu.com\api\protected\modules\ajax\controllers\MenuController.php

(24)

in C:\xampp\htdocs\emenu.com\api\index.php (13)

15:27:41.329886	trace	system.db.ar.CActiveRecord	

Foodtypes.findAll()

in

C:\xampp\htdocs\emenu.com\api\protected\modules\ajax\controllers\MenuController.php

(24)

in C:\xampp\htdocs\emenu.com\api\index.php (13)

15:27:41.331985	trace	system.db.CDbCommand	

Querying SQL: SELECT * FROM `tbl_foodtypes` `t`

in

C:\xampp\htdocs\emenu.com\api\protected\modules\ajax\controllers\MenuController.php

(24)

in C:\xampp\htdocs\emenu.com\api\index.php (13)

UPDATE: Just one last thing; I used Gii to create the AR Models. It must be working in my environment since the Phonebook demo works, so is it possibly a Gii Model Generation problem ?

2077

Foodtypes.php

Turns out I’m not able to access the data the same way as the documentation; if someone would tell me why, I’d be grateful :) To get the records, I had to the following:


    

    $ftypes = Foodtypes::model()->findAll();

    foreach($ftypes as $ftype) {        

        print_r( $ftype->getAttributes() );

    }              



wow, not a very responsive forum

From your example it’s hard to see any error, it’s only basic code and has no errors. You must have a typo or misconfiuration somewhere. Also you should tell more about what type of objects you actually get back.

Thanks for replying Mike.

I’m getting a Foodtypes Object with table schema and had to resort to doing this (inside Foodtypes class):




   public function getAll() {

  

      $data = Foodtypes::model()->findAll(); $ftypes = array();

      print_r($data);

      foreach($data as $ftype) {

          $ftypes[] = $ftype->attributes;

      }                  

      return $ftypes;

  }




But I’m pretty sure that isn’t necessary, based off of the demo examples and the tutorial:

http://www.yiiframework.com/doc/guide/1.1/en/database.ar#reading-record

Any idea what’s wrong?

I’m using the Gii model generator, which is btw, nice :)

What do you expect? findAll() will you return an array of ActiveRecord objects. You should:




foreach($data as $ftype)

  echo $ftype->whatever_attribute;

Don’t print_r() on these objects - it will only recurse through referenced other objects, e.g. the meta information or the db connection object.

I was originally expecting what I saw in the Phonebook demo and have pasted at the top of this thread.

How were they able to return/display such a simple result ??

I never really looked at that demo until now :). But this is rather a demo on how to write a web service with Yii. They use a SoapClient, to demonstrate how to consume such a service. So what you see is the response from that client, not from findAll().

Ahhhh, I see, thanks. Close this thread then, if you guys do that, and thank you