symfony / symfony/maker-bundle

“make:entity --regenerate” creates an incorrect (?) function

Open
#369 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
3.4k
Forks
427
Avg merge
2d 22h
Merged PRs (30d)
20

Description

I'm currently following a Symfony tutorial, and I've gotten to the part of Doctrine bidirectional relations (sorry if the terms I'm using are wrong, I'm not an native English speaker). My model is based on an Advert (One-To-Many) that displays an array of Applications (Many-To-One) made to this Advert. So an Application has to be linked to an Advert, hence the nullable false :

class Application
{
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Advert", inversedBy="applications")
     * @ORM\JoinColumn(nullable=false)
     */
    private $advert;

    //
}

And I added an $applications attribute to my Advert class:

class Advert
{ 
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Application", mappedBy="advert")
     */
    private $applications;

    //
}

But when I use php bin/console make:entity --regenerate, to get the removeApplication() function, the code I'm getting is the following:

public function removeApplication(Application $application): self
{
    if ($this->applications->contains($application)) {
        $this->applications->removeElement($application);
        // set the owning side to null (unless already changed)
        if ($application->getAdvert() === $this) {
            $application->setAdvert(null);
        }
    }

    return $this;
}

The function sets the $advert of the Application to null while this attribute is explicitly set to nullable = false. I noticed this inconsistency because I'm using Symfony 4 whereas the tutorial I'm following is based on an older version, so the functions generated in the tutorial were much simpler and did not handle the $advert attribute.

Any idea why this is happening and if it might cause an error later in my project? Let me know if you need more pieces of code to understand the problem.

Original question: https://stackoverflow.com/questions/55006548/makeentity-regenerate-creates-an-incorrect-function/55017382#55017382

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior with php bin/console make:entity --regenerate using the Advert and Application relationship shown in the issue, then inspect the regeneration entry point and generated removeApplication() method. Determine whether the generated nullable handling is intentional or incorrect, and document the expected behavior with a regression test if the command is at fault.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.