Up to this point I've been mainly using Yii's fantastic ActiveRecord features for all of my database querying and manipulation needs. I'm working on something now, however, that requires more complex queries and so have started using CDbConnection and CDbCommand to perform my queries. For example, as a very basic example, I'm simply doing something like this:
$connection = Yii::app()->db; $connection->active = true; $command = $connection->createCommand($sql); $rows = $command->queryAll();
The queries I am running are for "reports" made against data tables. While the sql for each report can vary, the same basic requirements exist for each: collect filter and criteria data from the user, build an sql statement, execute the query, return results to view for output in tabular form.
To make my job easier, I'm attempting to create a generic Report class (which extends CModel) that contains all of the core logic for building the report sql, including filters and sorts, and returning the results for display in the view. That way, instead of writing the same code over and over in each action of my controller, I simply need to instantiate a Report object, pass in filter options, and ask for the results back. What I would like to do could look something like this:
// instantiate new report object $report = new Report(); // pass in base sql without criteria $report->setSql($sql); // create criteria object based on user input $criteria = new CDbCriteria; $criteria->order = 'field1'; $criteria->condition = "$attribute = :fvalue"; $criteria->params = array(":fvalue"=>"$filtervalue"; // get resultset based on criteria $rows = $report->findAll($criteria);
The actual CDbConnection and CDbCommand would then be instantiated and used inside of the Report object. However, from what I can tell, neither CDbCommand or CDbConnection use CDbCriteria. It seems that the only way to apply some sort of filter or criteria to the sql is to manually concatenate the sql string and then use bindParam to replace placeholders with actual values.
Is there a way to take advantage of CDbCriteria when using CDbCommand? I've also seen the CDbCommandBuilder class that looks like it could also do what I want, but again, I don't see how CDbCommandBuilder is or can be used with CDbCommand. Any ideas or suggestions? Thanks in advance.