maker-bundle icon indicating copy to clipboard operation
maker-bundle copied to clipboard

Make entity regenerate creates relational getter setters for the incorrect entity

Open dant89 opened this issue 3 years ago • 0 comments

PHP: PHP 8.1.11 (cli) (built: Sep 29 2022 22:28:49) (NTS) symfony/maker-bundle : "v1.47.0"

When running php bin/console make:entity --regenerate to create the getters and setters for a one-to-many, the getters and setters wrongly use functions referencing the child class.

For example:

Continent.php

#[ORM\Entity]
class Continent
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(unique: true)]
    private string $name;

    #[ORM\OneToMany(
        mappedBy: "country",
        targetEntity: "Country",
        cascade: ["persist", "remove", "merge"],
        orphanRemoval: true)
    ]
    private $countries = [];

Country.php

#[ORM\Entity]
class Country
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(unique: true)]
    private string $name;

    #[ORM\ManyToOne(targetEntity: "Continent", cascade: ["all"], fetch: "EAGER")]
    private Continent $continent;

php bin/console make:entity --regenerate creates:

Continent.php

    public function addCountry(Country $country): self
    {
        if (!$this->countries->contains($country)) {
            $this->countries->add($country);
            $country->setCountry($this); // FIXME should be setContinent
        }

        return $this;
    }

    public function removeCountry(Country $country): self
    {
        if ($this->countries->removeElement($country)) {
            // set the owning side to null (unless already changed)
            if ($country->getCountry() === $this) { // FIXME should be getContinent
                $country->setCountry(null); // FIXME should be setContinent
            }
        }

        return $this;
    }

This file above uses these methods

  • $country->setCountry($this);
  • $country->getCountry()
  • $country->setCountry(null);

Instead it should be targeting the current class:

  • $country->setContinent($this);
  • $country->getContinent()
  • $country->setContinent(null);

dant89 avatar Oct 07 '22 11:10 dant89