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

Adding new user using base User entity and mapper fails #50

Closed
visto9259 opened this issue Mar 6, 2024 · 0 comments · Fixed by #55
Closed

Adding new user using base User entity and mapper fails #50

visto9259 opened this issue Mar 6, 2024 · 0 comments · Fixed by #55
Assignees
Labels
bug Something isn't working

Comments

@visto9259
Copy link
Member

@matwright

When using the off-the-shelf LmcUser, i.e. using the default \LmcUser\Entity\User class and the default LmcUser\Mapper\User class, the mapper fails when inserting a new user in the user table with the exception:

Laminas\Db\Adapter\Exception\InvalidQueryException
File:
C:\enofily\tutorials\lmc-user-tutorial\vendor\laminas\laminas-db\src\Adapter\Driver\Pdo\Statement.php:223
Message:
Statement could not be executed (23000 - 19 - UNIQUE constraint failed: user.user_id)

I am surprised that no one has ever reported this error before. Probably due to the fact that most people do not use the default classes.

The error is in the setId() method of the \LmcUser\Entity\User class:

    /**
     * Set id.
     *
     * @param  int $id
     * @return UserInterface
     */
    public function setId($id)
    {
        $this->id = (int) $id;
        return $this;
    }

When the $id parameter is null, as it will be when creating a new user from, for example, the register form, then $this->id is set 0. Then when inserting into the database, this id conflicts with the existing user rows that have a user_id of 0.

I understand that the (int) casting would be useful to convert an $id that is not an integer. But null should remain null and not be converted to 0.

Unless you know of other cases where we need to cast a null to 0, I will make the change to this method to set the $id property to null when the $id param is null.

visto9259 added a commit to visto9259/LmcUser that referenced this issue Mar 12, 2024
@visto9259 visto9259 self-assigned this Mar 12, 2024
@visto9259 visto9259 added the bug Something isn't working label Mar 12, 2024
@visto9259 visto9259 moved this from 🆕 New to 🏗 In progress in LmcUser Development Mar 12, 2024
visto9259 added a commit that referenced this issue Mar 14, 2024
Fix issue #50, update to GH actions name and README badges
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in LmcUser Development Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant