ikkez / f3-cortex

A multi-engine ORM / ODM for the PHP Fat-Free Framework
GNU General Public License v3.0
118 stars 22 forks source link

`filter()` with binding + `countRel()` does not bind parameters #99

Closed kumy closed 4 years ago

kumy commented 4 years ago

Here is another one…

When using filter() with bind values, followed by a countRel(), then the filter parameters are not bound.

Some simple examples simplified to be displayed here:


// Test 1 OK
        $lastPosition = new Move();
        $lastPosition->countRel('comments');
        $lastPosition->load();

// Test 2 OK
        $lastPosition = new Move();
        $lastPosition->filter('comments', ['type = ?', 1]);
        $lastPosition->load();

// Test 3 OK
        $lastPosition = new Move();
        $lastPosition->filter('comments', ['type = 1']); // No binding
        $lastPosition->countRel('comments');
        $lastPosition->load();

// TEST 4, the one I wanted to use, which fails
        $lastPosition = new Move();
        $lastPosition->filter('comments', ['type = ?', 1]);
        $lastPosition->countRel('comments');
        $lastPosition->load();
// PDOStatement: ERROR: bind message supplies 0 parameters, but prepared statement "pdo_stmt_00000009" requires 1
kumy commented 4 years ago

BTW, I've open it here as this is a bug, but technically, I'm thinking about moving this logic in my application from php down to Postgres procedural language as as TRIGGER.

kumy commented 4 years ago

All examples in the docs for countRel relates to the find() function. is it incompatible with load()?

https://github.com/ikkez/f3-cortex#find

// find published #web-design news, sorted by approved user comments
$news->has('tags',array('slug = ?','web-design'));
$news->filter('comments', array('approved = ?',1));
$news->countRel('comments');
$records = $news->find(
    array('publish_date <= ? and published = ?', date('Y-m-d'), true),
    array('order' => 'count_comments desc')
);

Confirmed with my example, using find() doesn't raise the error.

        $lastPosition = new Move();
        $lastPosition->filter('comments', ['type = ?', 1]);
        $lastPosition->countRel('comments');
        $lastPosition->find();
ikkez commented 4 years ago

have you tried the latest dev version or which one are you using?

kumy commented 4 years ago

I'm on 1.6.0, will test dev branch…

kumy commented 4 years ago

Unfortunately, same problem.

# composer  require ikkez/f3-cortex=dev-master
[…]
  - Updating ikkez/f3-cortex (v1.6.0 => dev-master 364caaa):  Checking out 364caaa151
PDOStatement: ERROR: bind message supplies 0 parameters, but prepared statement "pdo_stmt_00000007" requires 1
ikkez commented 4 years ago

Does it make a difference when you set count_rel first and call filter afterwards?

ikkez commented 4 years ago

please check latest commit

kumy commented 4 years ago

please check latest commit

Bad news :disappointed:

  - Updating ikkez/f3-cortex dev-master (364caaa => 90dd8b5):  Checking out 90dd8b5376

// PDOStatement: ERROR: bind message supplies 0 parameters, but prepared statement "pdo_stmt_00000005" requires 1

Do you need some traces? var_dump some variables?


Does it make a difference when you set count_rel first and call filter afterwards?

Yes, it does not raise exception. (tested on 90dd8b5376 and 1.6.0)

        $lastPosition = new Move();
        $lastPosition->countRel('comments');
        $lastPosition->filter('comments', ['type = ?', 1]);
        $lastPosition->load();
// No exception
ikkez commented 4 years ago

Fixed now... you could even add multiple and use different conditions for the count and the filter methods:

$lastPosition->filter('comments', ['type = ?', 1]);
$lastPosition->countRel('comments');
$lastPosition->countRel('comments', 'count_comments_type2', ['type = ?', 2]);
$lastPosition->load();