1

I have just installed Doctrine DBAL into a simple test PHP project and created a test database and got some queries happening successfully. Including queries that use both positional and named parameters eg:

echo '<br/>';
echo 'Simple SELECT WHERE Query using prepaired statements with positional paramaters:<br/>';
$Users_FirstName = 'Bill';
$sql = "SELECT * FROM users WHERE Users_FirstName = ?";
$stmt = $conn->prepare($sql);
$stmt->bindValue(1, $Users_FirstName);
$stmt->execute();
while ($row = $stmt->fetch()) {
    echo $row['Users_FirstName'].'<br/>';
}

echo '<br/>';
echo 'Simple SELECT WHERE Query using prepaired statements with named paramaters:<br/>';
$Users_FirstName = 'Bill';
$sql = "SELECT * FROM users WHERE Users_FirstName = :Users_FirstName";
$stmt = $conn->prepare($sql);
$stmt->bindValue('Users_FirstName', $Users_FirstName);
$stmt->execute();
while ($row = $stmt->fetch()) {
    echo $row['Users_FirstName'].'<br/>';
}

All the above works no problems. Now I am trying to execute a simple stored proc.
The target db is MySQL 5.1.37. The Stored Proc is as follows:

delimiter //
DROP PROCEDURE IF EXISTS TestProcedure//
CREATE PROCEDURE TestProcedure(INOUT TestParam VARCHAR(50)) 
BEGIN 
    SELECT CONCAT('Hi ', TestParam, '!!') INTO TestParam; 
END//

I have tested this in MySQL with the following SQL code:

delimiter ;
SET @testParam = 'Bill';
CALL `TestProcedure`(@testParam);
SELECT @testParam;

And it correctly returns the result of

'Hi Bill!!'

Now I am trying to execute the same stored proc form PHP using Doctrine DBAL with the following code:

echo '<br/>';
echo 'Call Stored Procedure with INOUT Parm:<br/>';
$INOUTParam = 'Bill';
$sql = "CALL TestProcedure(?)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $INOUTParam);
$stmt->execute();
echo $INOUTParam;

However this returns the following error:

Call Stored Procedure with INOUT Parm: exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1414 OUT or INOUT argument 1 for routine phptestdb.TestProcedure is not a variable or NEW pseudo-variable in BEFORE trigger' in [PATH REMOVED]\Doctrine\DBAL\Statement.php:138 Stack trace: #0

I have searched all over the place and can not find any example of calling a stored proc using bindParam with INOUT or OUT parameters. Does anyone know if Doctrine has support for stored procs, and if so how do you call one? Can can not find any examples on their site.

Regards,

Scott.

user2109254
  • 1,709
  • 2
  • 30
  • 49

2 Answers2

0

Native SQL is a option and you could even use stored procedures for data retrieval.

http://www.doctrine-project.org/blog/doctrine2-native-queries.html

or also you can use triggers in MySQL. Triggers wouldn't involve any coding in Doctrine, symfony nor PHP. Just stored procedures. For that check out Doctrine's record listeners or record hooks.

http://docs.doctrine-project.org/projects/doctrine1/en/latest/index.html#record-listeners http://docs.doctrine-project.org/projects/doctrine1/en/latest/index.html#record-hooks

I hope it helps..

medina
  • 8,051
  • 4
  • 25
  • 24
  • yeah I saw that post... but it still doesn't tell me if Doctrine supports stored procs? Native SQL would suggest it is database specific... I thought the idea of the Database Abstraction Layer is that you don't have to change all your queries to change your target database. If they are native queries, doesn't that defeat the purpose? – user2109254 Apr 04 '13 at 06:37
  • Ok finding that the underlying issue that causes the error, is caused by an bug in MySQL. See Here: http://stackoverflow.com/questions/118506/stored-procedures-mysql-and-php/4502524#4502524 there is a link to here: http://bugs.mysql.com/bug.php?id=11638 and as far as I can see, it was identified in 2005, and never fixed up to MySQL 5.6.10, as that is the last version I tested with. – user2109254 Apr 05 '13 at 03:51
  • @user2109254 , I dont think you'll find a database agnostic abstraction possible. Stored procedures/functions, views and so on are implemented in completely different ways from db to db such that its unlikely any abstraction would be sufficient to represent them all. For instance in postgres a stored procedure can be used as a subquery in a select statement. In mysql this isnt possible requiring you instead to use a hack like creating a temporary table to bridge the queries. Such is life. – Shayne Jan 06 '16 at 01:58
0

Ok finding that the underlying issue that causes the error, is caused by an bug in MySQL.

See Here: Stored Procedures, MySQL and PHP

There is a link to here: http://bugs.mysql.com/bug.php?id=11638

And as far as I can see, it was identified in 2005, and never fixed up to MySQL 5.6.10, as that is the last version I tested with.

Hope this helps someone else ;-)

Community
  • 1
  • 1
user2109254
  • 1,709
  • 2
  • 30
  • 49