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

make:entity wrong setter for non nullable fields #198

Open
sbruev opened this issue Jun 10, 2018 · 6 comments
Open

make:entity wrong setter for non nullable fields #198

sbruev opened this issue Jun 10, 2018 · 6 comments
Labels
Bug Bug Fix

Comments

@sbruev
Copy link

sbruev commented Jun 10, 2018

Hi!
It's a minor syntax mistake, but right now generated setters allow to pass the null value to non nullable fields, if they are ManyToOne relationship.

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\AnotherEntity")
     * @ORM\JoinColumn(nullable=false)
     */
    private $anotherEntity;

    //...

    public function setAnotherEntity(?AnotherEntity $anotherEntity): self
    {
        $this->anotherEntity = $anotherEntity;

        return $this;
    }

The arg should be AnotherEntity not ?AnotherEntity

Cheers!

@weaverryan weaverryan added the Bug Bug Fix label Jun 10, 2018
@weaverryan
Copy link
Member

Hmm, I was very careful about these details, but indeed, I can’t think of why I would have done this intentionally. Looks like a bug - thanks for the report :)

@lenar
Copy link

lenar commented Jun 21, 2018

Are you sure this is a bug? This would allow to use validations, otherwise the failure occurs too early.
I would say that currently there is a bug with ordinary fields which don't have ? before type if nullable=false. Failure occurs too early, makes it hard to validate. Maybe it should be configurable if one wants to allow nulls or not for setters.

@weaverryan
Copy link
Member

Hmm, interesting. Are you using the form submit, and it’s submitting the values as null?

@lenar
Copy link

lenar commented Jul 5, 2018

No, not using form submit. "Legacy" code, build with older Symfony releases. Manual validations with Symfony validation/constraint classes.

Anyway it should be selectable (by argument to console command) if you want very strict definitions (nulls not allowed if nullable=false and thus fail with PHP error) or you want to handle things yourself later with validations (and this seems to be the default with older releases and doctrine:generate:entitites).

And I really prefer handling nulls with validations and not manually modifying string => ?string in setters of generated entities.

@lenar
Copy link

lenar commented Jul 5, 2018

Looking at older (doctrine:generate:..) generated entities, the setters are setSomething($something = null) irrelevant of nullable of column annotation. They were generated automatically. And it makes sense IMHO.

@javiereguiluz
Copy link
Member

Even if this is correct in theory ... I'm afraid that in practice, when using Symfony Forms, is not correct. You must make everything nullable or the app won't work. That's why we did that in the Symfony Demo app for example: https://github.com/symfony/demo/blob/master/src/Entity/Post.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix
Projects
None yet
Development

No branches or pull requests

4 participants