Getting specific data from database

Posted by ifsession on Stack Overflow See other posts from Stack Overflow or by ifsession
Published on 2012-05-30T16:38:56Z Indexed on 2012/05/30 16:40 UTC
Read the original article Hit count: 97

Filed under:

I have a table called Categorie with a few columns and I'm trying to get only a few out of my database.

So I've tried this:

$sql = 'SELECT uppercat AS id, COUNT(uppercat) AS uppercat FROM categorie GROUP BY uppercat;';
$d = Yii::app()->db->createCommand($sql)->query();

But I find the output strange. I was trying to do an array_shift but I get an error that this isn't an array. When I do a var_dump on $d:

object(CDbDataReader)[38]
  private '_statement' => 
    object(PDOStatement)[37]
      public 'queryString' => string 'SELECT uppercat AS id, COUNT(uppercat) AS uppercat FROM categorie GROUP BY uppercat;' (length=100)
  private '_closed' => boolean false
  private '_row' => null
  private '_index' => int -1
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null

Ok.. then I did a foreach on $d:

array
  'id' => string '0' (length=1)
  'uppercat' => string '6' (length=1)
array
  'id' => string '3' (length=1)
  'uppercat' => string '2' (length=1)
array
  'id' => string '6' (length=1)
  'uppercat' => string '1' (length=1)
array
  'id' => string '7' (length=1)
  'uppercat' => string '2' (length=1)
array
  'id' => string '9' (length=1)
  'uppercat' => string '2' (length=1)

Then why do I get the message that $d isn't an array while it contains arrays?

Is there any other way on how to get some specific data out of my database and that I can then do an array_shift on them? I've also tried doing this with findAllBySql but then I can't reach my attribute for COUNT(uppercat) which is not in my model. I guess I'd have to add it to my model but I wouldn't like that because I need it just once.

© Stack Overflow or respective owner

Related posts about yii