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

Primary Keys are resolving to arrays even when they're not, which makes select->count() not work properly🐛 #58

Open
1 task done
longshadowdev opened this issue Sep 14, 2022 · 0 comments
Assignees
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug

Comments

@longshadowdev
Copy link

No duplicates 🥲.

  • I have searched for a similar issue in our bug tracker and didn't find any solutions.

What happened?

When I generate my schema using annotations I have noticed that it will set an integer auto-increment primary key with name $id to an array.

Entity PK definition:

#[Column(type: "primary")]
private int $id;

After persisting the schema to a json file:

        "7": [
            "id"
        ],

It looks like Cycle\Schema\Compiler is the culprit. Line 90 is Schema::PRIMARY_KEY => $entity->getPrimaryFields()->getNames(), and this always returns an array.

The IMPACT of this (at least the one I've seen), is seen in Cycle\ORM\Select::count(). If the $column parameter isn't set, it looks at the primary key definition instead as follows:

        if ($column === null) {
            // @tuneyourserver solves the issue with counting on queries with joins.
            $pk = $this->loader->getPK();
            $column = \is_array($pk)
                ? '*'
                : \sprintf('DISTINCT(%s)', $pk);
        }

        return (int) $this->__call('count', [$column]);

because it's always an array, it always puts in * instead of the DISTINCT() call. In a left join it will then show an incorrect count. It'll be showing the number of results in the SQL instead of the number of results in the SQL for the left/primary table.

I've confirmed this by changing the schema to not be an array, and then the count works accurately. I've also tried select->count('DISTINCT(table.id)'); and this works too (as it emulates what would happen if this was working correctly).

Version

    "cycle/orm": "^2.2",
    "cycle/annotated": "^3.2",
    "cycle/schema-builder": "^2.1",
    "cycle/entity-behavior": "^1.1",

php 8.0.16

ORM Schema

No response

@longshadowdev longshadowdev added status:to be verified Needs to be reproduced and validated. type:bug Bug labels Sep 14, 2022
@roxblnfk roxblnfk added this to Cycle Sep 14, 2022
@roxblnfk roxblnfk moved this to Todo in Cycle Sep 14, 2022
@roxblnfk roxblnfk added status:ready for adoption Feel free to implement this issue. and removed status:to be verified Needs to be reproduced and validated. labels Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug
Projects
Status: Todo
Development

No branches or pull requests

2 participants