Forked from mindplay-dk/ActiveRecord_createCommand.php
Last active
January 3, 2016 12:19
-
-
Save dfurber/8462163 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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