Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RecordExists and NoRecordExists is not reusable for validate different values #16

Closed
weierophinney opened this issue Dec 31, 2019 · 3 comments
Assignees
Labels
Won't Fix This will not be worked on
Milestone

Comments

@weierophinney
Copy link
Member

Custom modification in method for debug: https://github.com/zendframework/zend-validator/blob/master/src/Db/AbstractDb.php#L321

    protected function query($value)
    {
        $sql = new Sql($this->getAdapter());
        $select = $this->getSelect();
        $statement = $sql->prepareStatementForSqlObject($select);
        $parameters = $statement->getParameterContainer();
        $parameters['where1'] = $value; // <- PROBLEM
        echo $statement->getSql() . "\n"; // <- DEBUG
        $result = $statement->execute();
        return $result->current();
    }

test case:

        $validator = new RecordExists([
            'table'   => 'tags',
            'field'   => 'id',
            'adapter' => $this->getEvent()->getApplication()->getServiceManager()->get('DBSiteAdapter')
        ]);
        print_r($validator->isValid(1)); echo "\n";
        print_r($validator->isValid(2)); echo "\n";

result:

SELECT "tags"."id" AS "id" FROM "tags" WHERE "id" = :where1
1
SELECT "tags"."id" AS "id" FROM "tags" WHERE "id" = :where2
PDO EXCEPTION: SQLSTATE[HY093]: Invalid parameter number: :where1

as we see, in next call we have :where2 instead :where1


Originally posted by @AndrejAndb at zendframework/zend-validator#178

@weierophinney
Copy link
Member Author

Hm, I was found that problem occurs not for All PDO drivers. For sqlite: - all fine, but for pgsql: problem was present.

(Sorry, do not have time for continue analyze, I will fix it for me by implement alternative approach)


Originally posted by @AndrejAndb at zendframework/zend-validator#178 (comment)

@weierophinney
Copy link
Member Author

I will try to continue :)

source code of validator: https://github.com/zendframework/zend-validator/blob/2868a943818de387c1747f2ca966d4914e18bebb/src/Db/AbstractDb.php#L315-L325

    protected function query($value)
    {
        $sql = new Sql($this->getAdapter());
        $select = $this->getSelect();
        $statement = $sql->prepareStatementForSqlObject($select);
        $parameters = $statement->getParameterContainer();
        $parameters['where1'] = $value;
        $result = $statement->execute()

source code of https://github.com/zendframework/zend-db/blob/b1c79192c76b817062c1fa4e81ef2dcf1afea73e/src/Sql/AbstractSql.php#L136

        if (! isset($this->instanceParameterIndex[$namedParameterPrefix])) {
            $this->instanceParameterIndex[$namedParameterPrefix] = 1;
        }
        $expressionParamIndex = &$this->instanceParameterIndex[$namedParameterPrefix];

        .....

        $name = $namedParameterPrefix . $expressionParamIndex++;

so, for Select Object we have ever-increased number postfix for named parameter markers: where1, where2, etc...

But for sqlite is Ok, because Zend\Db\Sql\Platform\Platform has Select decorator for sqlite, and Platform object was re-instantiated for each 'validation call', and in this case we have that $this->instanceParameterIndex reset each time

:)

I am not sure that it is must be fixed on zend-db side, looks like better implement some alternative approach for validator :)


Originally posted by @AndrejAndb at zendframework/zend-validator#178 (comment)

@gsteel
Copy link
Member

gsteel commented Jun 25, 2024

Closed via #249

@gsteel gsteel closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Won't Fix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants