Skip to content
This repository

Add support CDbCommand in CSqlDataProvider #1500

Merged
merged 3 commits into from over 1 year ago

4 participants

Veaceslav Medvedev Alexander Kochetov Carsten Brandt Alexander Makarov
Veaceslav Medvedev

Add support CDbCommand in CSqlDataProvider

Example controller action:

$command = Yii::app()->db->createCommand();
// use query builder
$command->where(...);
// may be need
$command->setFetchMode(PDO::FETCH_OBJ);

$dataProvider = new CSqlDataProvider($command, $config);
Alexander Kochetov

Very useful feature. Vote for merge this.

CHANGELOG
@@ -55,6 +55,7 @@ Version 1.1.13 work in progress
55 55
 - Enh: Value of the CHtml::activeTextArea() can now be set through $htmlOptions['value'] (resurtm)
56 56
 - Enh: Allow to customize CHtml::error() container tag (creocoder)
57 57
 - Enh: CModule::setComponents() now can reconfigure already loaded components (creocoder)
  58
+- Enh #1500: CSqlDataProvider now supports CDbCommand in constructor (slavcodev)
1
Alexander Makarov Collaborator
samdark added a note October 03, 2012

Should be ordered according to Enh #.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Carsten Brandt cebe merged commit 47c9ddb into from October 10, 2012
Carsten Brandt cebe closed this October 10, 2012
Carsten Brandt
Collaborator

merged and added some docs, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  CHANGELOG
@@ -48,6 +48,7 @@ Version 1.1.13 work in progress
48 48
 - Enh #1396: Added 'text/csv' mime-type for the 'csv' file extension in utils/mimeTypes.php (effectively used by e.g. CHttpRequest::sendFile()) (rawtaz)
49 49
 - Enh #1426: Behaviors are now affecting memory consumption significantly less (slavcodev, creocoder, Qiang, samdark)
50 50
 - Enh #1443: Added CHttpRequest::getRawBody() that allows reading RAW HTTP request body multiple times (itamar82, resurtm, samdark)
  51
+- Enh #1500: CSqlDataProvider now supports CDbCommand in constructor (slavcodev)
51 52
 - Enh: Fixed the check for ajaxUpdate false value in jquery.yiilistview.js as that never happens (mdomba)
52 53
 - Enh: Requirements checker: added check for Oracle database (pdo_oci extension) and MSSQL (pdo_dblib, pdo_sqlsrv and pdo_mssql extensions) (resurtm)
53 54
 - Enh: Added CChainedLogFilter class to allow adding multiple filters to a logroute (cebe)
23  framework/web/CSqlDataProvider.php
@@ -43,7 +43,7 @@ class CSqlDataProvider extends CDataProvider
43 43
 	 */
44 44
 	public $db;
45 45
 	/**
46  
-	 * @var string the SQL statement to be used for fetching data rows.
  46
+	 * @var string|CDbCommand the SQL statement to be used for fetching data rows.
47 47
 	 */
48 48
 	public $sql;
49 49
 	/**
@@ -57,7 +57,7 @@ class CSqlDataProvider extends CDataProvider
57 57
 
58 58
 	/**
59 59
 	 * Constructor.
60  
-	 * @param string $sql the SQL statement to be used for fetching data rows.
  60
+	 * @param string|CDbCommand $sql the SQL statement to be used for fetching data rows.
61 61
 	 * @param array $config configuration (name=>value) to be applied as the initial property values of this class.
62 62
 	 */
63 63
 	public function __construct($sql,$config=array())
@@ -73,19 +73,23 @@ public function __construct($sql,$config=array())
73 73
 	 */
74 74
 	protected function fetchData()
75 75
 	{
76  
-		$sql=$this->sql;
77  
-		$db=$this->db===null ? Yii::app()->db : $this->db;
78  
-		$db->active=true;
  76
+		if(!$this->sql instanceof CDbCommand)
  77
+		{
  78
+			$db=$this->db===null ? Yii::app()->db : $this->db;
  79
+			$command=$db->createCommand($this->sql);
  80
+		}
  81
+		else
  82
+			$command=clone $this->sql;
79 83
 
80 84
 		if(($sort=$this->getSort())!==false)
81 85
 		{
82 86
 			$order=$sort->getOrderBy();
83 87
 			if(!empty($order))
84 88
 			{
85  
-				if(preg_match('/\s+order\s+by\s+[\w\s,]+$/i',$sql))
86  
-					$sql.=', '.$order;
  89
+				if(preg_match('/\s+order\s+by\s+[\w\s,]+$/i',$command->text))
  90
+					$command->text.=', '.$order;
87 91
 				else
88  
-					$sql.=' ORDER BY '.$order;
  92
+					$command->text.=' ORDER BY '.$order;
89 93
 			}
90 94
 		}
91 95
 
@@ -94,10 +98,9 @@ protected function fetchData()
94 98
 			$pagination->setItemCount($this->getTotalItemCount());
95 99
 			$limit=$pagination->getLimit();
96 100
 			$offset=$pagination->getOffset();
97  
-			$sql=$db->getCommandBuilder()->applyLimit($sql,$limit,$offset);
  101
+			$command->text=$command->getConnection()->getCommandBuilder()->applyLimit($command->text,$limit,$offset);
98 102
 		}
99 103
 
100  
-		$command=$db->createCommand($sql);
101 104
 		foreach($this->params as $name=>$value)
102 105
 			$command->bindValue($name,$value);
103 106
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.