Jump to content
Larry Ullman's Book Forums
Sign in to follow this  

Returning Objects Using Dao

Recommended Posts

Hi Larry,


When I test the DAO in your book: (page 186) 

$q = 'SELECT * FROM page WHERE id=1';
$cmd = Yii::app()->db->createCommand($q);
$cmd->setFetchMode(PDO::FETCH_CLASS, 'Page');
$model = $cmd->queryRow();
// Use $model->title et al.


I can not retrieve $model->title which is null. Also, I use $model-> attributes, the result is :

Array ( [live] => 0 [id] => [user_id] => [title] => [content] => [date_updated] => [date_published] => ) 


Strangely, only 'live' is not null. The expected result is:

Array ( [id] => 1 [user_id] => 1 [live] => 0 [title] => ewwq [content] => wsds [date_updated] => 2013-04-04 23:48:50 [date_published] => 2013-04-04 ) 


I use other tables and corresponding class to test, it also dose not work.

I really hope you can explain it. By the way, I really appreciate your work.


Thank You


Share this post

Link to post
Share on other sites

With that result, you need to use the result as an array, i.e $model['title'] instead of $model->title. According to someone one the YII forum, you need to switch from PDO::FETCH_CLASS to PDO::FETCH_OBJ.


You might need to bind that param too. I don't know. :)

Share this post

Link to post
Share on other sites
Thank you Antonio.

1. I have tried PDO::FETCH_OBJ, it returns an  stdClass Object. And it works

2. I tried  $model['title'] with PDO::FETCH_CLASS , it won't work, since $model is a Page AR object.


What I want to get is a Page AR object with correct properties using DAO(I know how to do the same thing using AR instead of DAO). Since Larry mentioned DAO has "the ability to fetch into specific object types".  And I really want to make it work ,but did not get the right answer <_< 


Again, thank you! :) 

Share this post

Link to post
Share on other sites

I thought that worked for me. If you're still having problems with this, let me know and I'll go back and see what I did.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...