I am going through the blog tutorial, step by step and it is working but one thing I am sort of taking for granted is the SQL methods in CActiveRecord.
I was thinking a great thing that would help me with using CActiveRecord methods is seeing the actual SQL querey that it generates. For example, in the tutorial we generate this function:
class Comment extends CActiveRecord
{
......
public function findRecentComments($limit=10)
{
return $this->with('post')->findAll(array(
'condition'=>'t.status='.self::STATUS_APPROVED,
'order'=>'t.create_time DESC',
'limit'=>$limit,
));
}
}
Now, I sort of understand what is going on… We are selecting all the comments, joining the Posts table, and limiting our querey to a limit of 10 and an approved status. We are also ordering them by date descending.
So…
SELECT * FROM `tbl_comment` c
LEFT JOIN `tbl_post` p ON p.post_id = c.id
WHERE c.status = 1 AND c.post_id = ## //(whatever post we are viewing)
ORDER BY c.create_time DESC
LIMIT 10
That’s just my best guess of the query that pops out of that method, but I don’t know.
If there was a way to log the actual executed queries in the debug mode, just to double check and be sure what we programming is right, that would be great!
As an aside, I do think Yii is taking an elegant approach to doing SQL and grabbing data. I am used to writing out my own statements, crafting them so they are just right through multiple tries on the console, and then putting them on my site. So, I am currently on the fence whether I should do my SQL the Yii way or do it my own way. I am thinking for the simple SQL needed that are non-intrusive, I would do it the Yii way. For more advanced queries, I’d do it manually. Any comments on this?