-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtendedConnection.php
66 lines (63 loc) · 2.08 KB
/
ExtendedConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/*
* Symfony2 Doctrine Meshup
*
* uses the doctrine db details, create new PDO connection
* (as doctrine wont return the actual resource, just a wrapped object)
* run the query and loop through reults.
*
* example:
* $sql = "EXEC someSP ?,?,?";
* $sqlParams = array('param1','param2', 'param3');
* $conn = $this->getDoctrine()->getConnection();
* $results = $conn->multipleResultSetsFetchAll($sql, $sqlParams);
*
* ensure you have added the wrapper to your DBAL config:
* doctrine:
* dbal:
* wrapper_class: 'Some\Bundle\Doctrine\DBAL\ExtendedConnection'
* driver: %database_driver%
* host: %database_host%
* port: %database_port%
* dbname: %database_name%
* user: %database_user%
* password: %database_password%
* MultipleActiveResultSets: true
*
* $sql should be
*/
namespace Some\Bundle\Doctrine\DBAL;
use Doctrine\DBAL\Connection AS Connection;
class ExtendedConnection extends Connection
{
/*
* executes SQL and returns All results (inc. multiple result sets)
* dynamic sql using ? as param place holders
*
* @param $sql string Native SQL
* @param $sqlParams array single dimension
*
* @return array multi dimension
*/
public function multipleResultSetsFetchAll($sql, $sqlParams)
{
//Get Connection infor from Doctrine
$params = $this->getParams();
//create new PDO as we can only access the Doctrine PDO as an object only
//via $this->getWrappedConnection()
$conn = new \PDO("sqlsrv:Server=".$params['host'].";Database=".$params['dbname'],$params['user'],$params['password']);
//prepare statement
$stmt = $conn->prepare($sql);
//execute statement with params
$stmt->execute($sqlParams);
//grab each result set
do{
$results[] = $stmt->fetchAll();
}while($stmt->nextRowset());
//close $stmt and $conn
$stmt->closeCursor();
unset($stmt , $conn);
//return
return $results;
}
}