Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

invoke execute() twice passing ParameterContainer, second execution uses old parameters #7280

Closed
lomo74 opened this issue Feb 28, 2015 · 1 comment
Labels

Comments

@lomo74
Copy link

lomo74 commented Feb 28, 2015

$stmt = $adapter->query("SELECT CAST(:num AS INT) AS num"); //first invocation $params = new ParameterContainer(); $params->offsetSet('num', 1); echo $stmt->execute($params)->current()['num'] . PHP_EOL; //second invocation $params = new ParameterContainer(); $params->offsetSet('num', 2); echo $stmt->execute($params)->current()['num'] . PHP_EOL;

output:
1
1

Looking at zend-db/Zend/Db/Adapter/Statement.php line 216 and subsequent:

/** START Standard ParameterContainer Merging Block */
if (!$this->parameterContainer instanceof ParameterContainer) {
if ($parameters instanceof ParameterContainer) {
$this->parameterContainer = $parameters;
$parameters = null;
} else {
$this->parameterContainer = new ParameterContainer();
}
}

    if (is_array($parameters)) {
        $this->parameterContainer->setFromArray($parameters);
    }

    if ($this->parameterContainer->count() > 0) {
        $this->bindParametersFromContainer();
    }
    /** END Standard ParameterContainer Merging Block */

$this->parameterContainer is not overwritten, unless either
(a) it is not a ParameterContainer instance
(b) the $parameters passed to the function is an array

During the second invocation, neither of the above is true, so the new parameters are simply ignored and the old ones are used.

The above should look like this IMHO:

/** START Standard ParameterContainer Merging Block */
if ($parameters instanceof ParameterContainer) {
$this->parameterContainer = $parameters;
$parameters = null;
} else if (!$this->parameterContainer instanceof ParameterContainer) {
$this->parameterContainer = new ParameterContainer();
}

    if (is_array($parameters)) {
        $this->parameterContainer->setFromArray($parameters);
    }

    if ($this->parameterContainer->count() > 0) {
        $this->bindParametersFromContainer();
    }
    /** END Standard ParameterContainer Merging Block */
@GeeH
Copy link

GeeH commented Jun 28, 2016

This issue has been moved from the zendframework repository as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html
New issue can be found at: zendframework/zend-db#135

@GeeH GeeH closed this as completed Jun 28, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants