Skip to content

Commit

Permalink
Merge pull request #1500 branch 'CSqlDataProvider-support-CDbCommand'…
Browse files Browse the repository at this point in the history
… of https://github.com/slavcodev/fork-yii into slavcodev-CSqlDataProvider-support-CDbCommand

* 'CSqlDataProvider-support-CDbCommand' of https://github.com/slavcodev/fork-yii:
  CHAGELOG fix
  CHANGELOG
  CSqlDataProvider now supports CDbCommand in constructor

Conflicts:
	CHANGELOG
  • Loading branch information
cebe committed Oct 10, 2012
2 parents e65956d + 47c9ddb commit e8a93ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -48,6 +48,7 @@ Version 1.1.13 work in progress
- Enh #1396: Added 'text/csv' mime-type for the 'csv' file extension in utils/mimeTypes.php (effectively used by e.g. CHttpRequest::sendFile()) (rawtaz) - Enh #1396: Added 'text/csv' mime-type for the 'csv' file extension in utils/mimeTypes.php (effectively used by e.g. CHttpRequest::sendFile()) (rawtaz)
- Enh #1426: Behaviors are now affecting memory consumption significantly less (slavcodev, creocoder, Qiang, samdark) - Enh #1426: Behaviors are now affecting memory consumption significantly less (slavcodev, creocoder, Qiang, samdark)
- Enh #1443: Added CHttpRequest::getRawBody() that allows reading RAW HTTP request body multiple times (itamar82, resurtm, samdark) - Enh #1443: Added CHttpRequest::getRawBody() that allows reading RAW HTTP request body multiple times (itamar82, resurtm, samdark)
- Enh #1500: CSqlDataProvider now supports CDbCommand in constructor (slavcodev)
- Enh #1518: Allow to configure CHtml::$closeSingleTags and CHtml::$renderSpecialAttributesValue. Useful for HTML5 code (creocoder) - Enh #1518: Allow to configure CHtml::$closeSingleTags and CHtml::$renderSpecialAttributesValue. Useful for HTML5 code (creocoder)
- Enh #1531: CArrayDataProvider is now able to sort cutted array, where sorting column is not available in every entry (Yiivgeny) - Enh #1531: CArrayDataProvider is now able to sort cutted array, where sorting column is not available in every entry (Yiivgeny)
- Enh: Fixed the check for ajaxUpdate false value in jquery.yiilistview.js as that never happens (mdomba) - Enh: Fixed the check for ajaxUpdate false value in jquery.yiilistview.js as that never happens (mdomba)
Expand Down
23 changes: 13 additions & 10 deletions framework/web/CSqlDataProvider.php
Expand Up @@ -43,7 +43,7 @@ class CSqlDataProvider extends CDataProvider
*/ */
public $db; public $db;
/** /**
* @var string the SQL statement to be used for fetching data rows. * @var string|CDbCommand the SQL statement to be used for fetching data rows.
*/ */
public $sql; public $sql;
/** /**
Expand All @@ -57,7 +57,7 @@ class CSqlDataProvider extends CDataProvider


/** /**
* Constructor. * Constructor.
* @param string $sql the SQL statement to be used for fetching data rows. * @param string|CDbCommand $sql the SQL statement to be used for fetching data rows.
* @param array $config configuration (name=>value) to be applied as the initial property values of this class. * @param array $config configuration (name=>value) to be applied as the initial property values of this class.
*/ */
public function __construct($sql,$config=array()) public function __construct($sql,$config=array())
Expand All @@ -73,19 +73,23 @@ public function __construct($sql,$config=array())
*/ */
protected function fetchData() protected function fetchData()
{ {
$sql=$this->sql; if(!$this->sql instanceof CDbCommand)
$db=$this->db===null ? Yii::app()->db : $this->db; {
$db->active=true; $db=$this->db===null ? Yii::app()->db : $this->db;
$command=$db->createCommand($this->sql);
}
else
$command=clone $this->sql;


if(($sort=$this->getSort())!==false) if(($sort=$this->getSort())!==false)
{ {
$order=$sort->getOrderBy(); $order=$sort->getOrderBy();
if(!empty($order)) if(!empty($order))
{ {
if(preg_match('/\s+order\s+by\s+[\w\s,]+$/i',$sql)) if(preg_match('/\s+order\s+by\s+[\w\s,]+$/i',$command->text))
$sql.=', '.$order; $command->text.=', '.$order;
else else
$sql.=' ORDER BY '.$order; $command->text.=' ORDER BY '.$order;
} }
} }


Expand All @@ -94,10 +98,9 @@ protected function fetchData()
$pagination->setItemCount($this->getTotalItemCount()); $pagination->setItemCount($this->getTotalItemCount());
$limit=$pagination->getLimit(); $limit=$pagination->getLimit();
$offset=$pagination->getOffset(); $offset=$pagination->getOffset();
$sql=$db->getCommandBuilder()->applyLimit($sql,$limit,$offset); $command->text=$command->getConnection()->getCommandBuilder()->applyLimit($command->text,$limit,$offset);
} }


$command=$db->createCommand($sql);
foreach($this->params as $name=>$value) foreach($this->params as $name=>$value)
$command->bindValue($name,$value); $command->bindValue($name,$value);


Expand Down

0 comments on commit e8a93ac

Please sign in to comment.