Inconsequent Column Quotes

Hey guys,

please tell me if I’m missing something.

Why are columns specified by a [color="#0000FF"]CDbCriteria::$select[/color] quoted if the query is build by [color="#0000FF"]CActiveFinder::getColumnSelect()[/color] but not if the query is build by [color="#0000FF"]CDbCommandBuilder::createFindCommand()[/color]? Both methods are used by [color="#0000FF"]CActiveRecord::query()[/color], the first one if [color="#0000FF"]CDbCriteria::$with[/color] was set the later if it’s just a single table being queried.

So following results in an MySQL error, as column "key" must be quoted:


User::model()->findByPk($pk, array(

    'select'=>array('key'),

));

But as soon as I join any relation, it will get quoted and thus will return the result:


User::model()->findByPk($pk, array(

    'select'=>array('key'),

    'with'=>array('profile'),

));

Sure, you should not be using MySQL keywords as column names in the first place. Still this behavior doesn’t really make sense, does it?

Thank you for your feedback!

David

That’s interesting. Sounds like a bug to me.

For now you can extend the [color="#0000FF"]CActiveRecord[/color] class and override [color="#0000FF"]CActiveRecord::query()[/color] like this to always use the [color="#0000FF"]CActiveFinder::query()[/color] method to build your query. It’s a bit of an overkill maybe, but it makes sure your ActiveRecord’s columns are always quoted.




protected function query($criteria, $all=false)

{

	$this->beforeFind();

	$this->applyScopes($criteria);


	if(!$all)

	{

		$criteria->limit=1;

	}


	if(empty($criteria->with))

	{

		$criteria->with=array();

	}


	$finder=$this->getActiveFinder($criteria->with);

	return $finder->query($criteria,$all);

}