Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfurber/8462163 to your computer and use it in GitHub Desktop.
Save dfurber/8462163 to your computer and use it in GitHub Desktop.
<?php
class ActiveRecord extends CActiveRecord
{
/**
* Create and configure a CDbCommand instance based on current criteria.
*
* Think twice before using this method - use only in cases where being able to stream
* through the raw SQL record-sets is crucial, usually for performance reasons, when
* dealing with very large record sets.
*
* This often will not do what you expect, at least not in the first attempt - Yii AR
* writes queries that are optimized for Yii, and not always fit for human consumption.
*
* if you're going to grab the raw SQL command with this method:
*
* - DON'T use CActiveRecord::together() - you won't get what you were expecting!
* - DO add custom CDbCriteria::$select clauses, since Yii garbles column names by
* default; you probably want to be selective about which columns you select anyway.
*
* It currently throws an exception if you use 'with'. It may work in some cases but I have not looking into it yet.
*
* @param boolean $all Return all the records? Default is true. Set to false to return the first record.
*
* @return CDbCommand DANGER DANGER, ENTER THE GRAVE YARD CHAMBER
* @see http://www.youtube.com/watch?v=auqur-Nz-X8
*/
public function createCommand($all=true)
{
$this->beforeFind();
if(empty($this->getDbCriteria()->with))
{
if(!$all)
$this->getDbCriteria()->limit=1;
return $this->getCommandBuilder()->createFindCommand($this->getTableSchema(),$this->getDbCriteria());
}
else
{
throw new CDbException("Cannot convert 'with' query to db command.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment