@basho What do you want to do exactly? Describe the use case in order to get an advice.
get statement variable for prepared pdo calls
Hi, i just started playing around with laravel and i wanted to check something about prepared statements.
What i want to do is get the $statement variable before fetch is called so that i can run it again with differen parameters. I have a select statement like this : Select * from table where primary_key = :var_primary_key so i bind the parameters like this
array(
"var_primary_key" => 1
);
and after that i want to run the same prepared statement again with another value for var_primary_key
But in the Connection.php File in the select function the $statement isn't returned
$statement->execute($me->prepareBindings($bindings));
return $statement->fetchAll($me->getFetchMode());
instead the fetchAll command is called.
So is there no support in laravel for the rerun of a prepared statement with different parameters?
My Solution at the moment is another function in the Connection.php Class But i'm not so keen to modify Core Classes, can i extend them in an easy way?
And of course it would be great to use this with the query builder like
$user = DB::table('users')->where('name', ':var_name')->returnPrepared();
Here is the function i use, would be cleaner to have fourth parameter at the select Function to return the statement.
public function selectPrepared($query, $bindings = array(), $useReadPdo = true)
{
return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
{
if ($me->pretending()) return array();
// For select statements, we'll simply execute the query and return an array
// of the database result set. Each element in the array will be a single
// row from the database table, and will either be an array or objects.
$statement = $this->getPdoForSelect($useReadPdo)->prepare($query);
$statement->execute($me->prepareBindings($bindings));
return $statement;
});
}
Did i overlook something?
@basho Well, you can't do that with the query builder of course, as you already noticed. I never needed that so far, but it seems to be useful in fact, so a PR maybe? :)
Anyway, to override Connection and Builder classes you need to bind the custom connection class in a non-deferred service provider. Then you will be able to use both Eloquent and the Query Builder without any adjustments, but also will you have your custom methods.
Here's an example, how you do it: http://softonsofa.com/laravel-query-builder-global-scope-how-to-use-custom-connection-and-query-builder-in-laravel-4/
Please or to participate in this conversation.