How to reuse SELECT part in QueryBuilder

I am trying to reuse the SELECT part of a query using the query builder.

The problem is that the query builder always quotes the column names, which makes it impossible to reuse the select part.

So … I create a new command, set the SELECT part, but later I have to modify the select part and add another column.

A simple example:







$cmd = Yii::app()->db->createCommand();

		

		$cmd->select( array( 'id', 'name', 'mail' ) );

		

		$strSelect = $cmd->getSelect();

		

		$strSelect .= ',tel';

		

		$cmd->select( $strSelect );

		

		$cmd->from( 'testTbl' );

		

		$text = $cmd->getText();




This code snippet will not work, because the columns will be quoted two times => invalid query.

Is there a way to disable column quoting?

I think a flag to disable it would be a good thing to have. Always quoting the column names is limiting IMHO.

Parsing the string and removing the quotes is an option, but its not a good solution.

Thanks in advance :)

Fields are automatically quoted… only if there are parenthesis in the select string they will not be quoted - http://www.yiiframework.com/doc/api/1.1/CDbCommand#select()-detail

never tried as I did not need it… but maybe you can use some parenthesis in teh select string… or just add a function like "now()"…

You did not explain the complete need…

but the best solution would be to build a string with the select fields…

and only in the end give that string to the select(), without using getSelect()

Hi mdomba,

Thanks for the reply.

Basically I work with a custom ORM framework, based on CModel and the QueryBuilder.

I want to extended the default behavior( in my case - add some columns, which all my models have ). To do that, I have to extend the function which prepares a CDbCommand instance, which is later used by the Load method. So its not possible to prepare the select first as string or array and pass it at the end only one time to $cmd->select(), because in my class I call the parent implementation which returns me the already created CDbCommand instance.

I ended up striping the slashes and preparing the query one more time and in my case it works, but its not the best solution.

If one has to do this more than one time and with a lot of columns, its a waste of valuable time.

Depending on the working of your class… you can have a private property in there that would hold the select value before quoting…

so every time you call yourclass->select() you first remember the values in a private variable and then call the parent->select()