That is a working but very ugly implementation. It would be better to take some time for study the class CActiveRecord and CDbCriteria. A wide part of complex sql queryes can be managed with this class using Yii methods.
If you need to use multiple table, you might need learn together, with or join method() in CDBCriteria. Or maybe if the query is too complex just use SQL Query. You can use CDbCommand to execute the query…
All the public function on the POST you can called from View by using this code:
//in the view
echo Post::model()->myMethod($param1, $param2);