How To Get Query Result As A Object Instead Of Array

Hi,

I have query in my application like this,

When I var_dump($result), I get the array … as below,

The $result is in array, but If we want to get result as a object,

how can I get?




$result = Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ)

 ->select('u.*')

 ->from('user u')

 ->where('u.role_id = :role_id', array(':role_id' => '1'))

 ->queryAll();



Hi thanks alex-w for your instant reply,

I tried above code &

var_dump($result);die;

,get the result as below,


array

  0 => 

    object(stdClass)[25]

      public 'user_id' => string '1' (length=1)

      public 'role_id' => string '1' (length=1)

      public 'company_id' => string '1' (length=1)

      public 'user_name' => string 'abc' (length=3)

      public 'password' => string '88809d5579b1bd03952a13d27d910888' (length=32)

      public 'email' => string 'abc@gmail.com' (length=13)

      public 'first_name' => string 'abc' (length=3)

      public 'last_name' => string 'abc' (length=3)

      public 'img' => string '' (length=0)

      public 'date_format' => string '' (length=0)

      public 'send_notifications' => string '' (length=0)

      public 'language' => string 'en' (length=2)

      public 'created_at' => string '2012-12-14' (length=10)

  1 => 

    object(stdClass)[26]

      public 'user_id' => string '2' (length=1)

      public 'role_id' => string '1' (length=1)

      public 'company_id' => string '1' (length=1)

      public 'user_name' => string 'xyz' (length=3)

      public 'password' => string 'd16fb36f0911f878998c136191af705e' (length=32)

      public 'email' => string 'xyz@gmail.com' (length=13)

      public 'first_name' => string 'xyz' (length=3)

      public 'last_name' => string 'xyz' (length=3)

      public 'img' => string '' (length=0)

      public 'date_format' => string '' (length=0)

      public 'send_notifications' => string '' (length=0)

      public 'language' => string '' (length=0)

      public 'created_at' => string '2012-12-14' (length=10)

Or I just print the array as, print_r($result);die;


Array ( [0] => stdClass Object ( [user_id] => 1 [role_id] => 1 [company_id] => 1 [user_name] => abc [password] => 88809d5579b1bd03952a13d27d910888 [email] => abc@gmail.com [first_name] => abc [last_name] => abc [img] => [date_format] => [send_notifications] => [language] => en [created_at] => 2012-12-14 ) [1] => stdClass Object ( [user_id] => 2 [role_id] => 1 [company_id] => 1 [user_name] => xyz [password] => d16fb36f0911f878998c136191af705e [email] => xyz@gmail.com [first_name] => xyz [last_name] => xyz [img] => [date_format] => [send_notifications] => [language] => [created_at] => 2012-12-14 ) ) 

So you can see here … we are geting array with indexing from "0" … not an object

When I want display userid, I have to do like this,


 echo $result[0]->user_id; 

so … here we are using indexed array… can I get this as a object…????

It wouldn’t work without being an array.

For it to be an object you would need named properties,

e.g.


$result->user1->

$result->user2->

Which then just complicates iteration.

It’s because you’re using queryAll(), which is returning an array of objects. Try using queryRow() if you’re only after one record.

As mentioned above, it doesn’t make sense to return the collection as a single object.

So… we will get collection of objects in array…

But if we want as below,


$result->user1->

$result->user2->

How can I get this???

thanks… Alex-w…

Thanks Keith,

For single record/row, queryRow() works perfectly as below,


$result = Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ)

				 ->select('u.*')

				 ->from('user u')

				 ->where('u.role_id = :role_id', array(':role_id' => '1'))

				 ->queryRow();

		echo $result->user_id;die;

Thanks…

So I can do this way,




$result = Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ)

				 ->select('u.*')

				 ->from('user u')

				 ->where('u.role_id = :role_id', array(':role_id' => '1'))

				 ->queryAll();

		

		foreach ($result as $row) {

    			echo $row->user_id;//instead of $row['column1']

    			echo $row->role_id;//instead of $row['column2']

    			echo $row->company_id;//instead of $row['column3']

				

		}

		die;




thanks Alex & Keith,

Yes, that’s how you’d process the records.

yup… thanks… :)