DDC-1825: generate entities with traits #2297

Closed
opened 2026-01-22 13:47:40 +01:00 by admin · 7 comments
Owner

Originally created by @doctrinebot on GitHub (May 18, 2012).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user lunetics:

When a trait with included setters and getters is used and generate entities is called, doctrine add another set of getters and setters to the "main" entity where the trait is used.

Originally created by @doctrinebot on GitHub (May 18, 2012). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user lunetics: When a trait with included setters and getters is used and generate entities is called, doctrine add another set of getters and setters to the "main" entity where the trait is used.
admin added the ImprovementWon't Fix labels 2026-01-22 13:47:40 +01:00
admin closed this issue 2026-01-22 13:47:41 +01:00
Author
Owner

@doctrinebot commented on GitHub (May 1, 2014):

Comment created by lsv20:

/****
 * @ORM\Entity
 */
class Product {
    use Traits\Created;

    /****
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /****
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /****
     * Set name
     *
     * @param string $name
     * @return Attribute
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /****
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

}
Trait Created {
    /****
     * @var \DateTime $created
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime")
     */
    private $created;

    /****
     * @return \DateTime
     */
    public function getCreated()
    {
        return $this->created;
    }
}

Now when I run php app/console doctrine:generate:entities it copies everything from the trait and into the entity, so the entity now looks like

/****
 * @ORM\Entity
 */
class Product {
    use Traits\Created;

    /****
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /****
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /****
     * Set name
     *
     * @param string $name
     * @return Attribute
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /****
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /****
     * @var \DateTime $created
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime")
     */
    private $created;

    /****
     * @return \DateTime
     */
    public function getCreated()
    {
        return $this->created;
    }

}

And ofcourse invalidates the entity because it now has two methods of the getCreated and two of private $created

@doctrinebot commented on GitHub (May 1, 2014): Comment created by lsv20: ``` /**** * @ORM\Entity */ class Product { use Traits\Created; /**** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /**** * @var string * * @ORM\Column(name="name", type="string", length=255) */ private $name; /**** * Set name * * @param string $name * @return Attribute */ public function setName($name) { $this->name = $name; return $this; } /**** * Get name * * @return string */ public function getName() { return $this->name; } } ``` ``` Trait Created { /**** * @var \DateTime $created * * @Gedmo\Timestampable(on="create") * @ORM\Column(type="datetime") */ private $created; /**** * @return \DateTime */ public function getCreated() { return $this->created; } } ``` Now when I run **php app/console doctrine:generate:entities** it copies everything from the trait and into the entity, so the entity now looks like ``` /**** * @ORM\Entity */ class Product { use Traits\Created; /**** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /**** * @var string * * @ORM\Column(name="name", type="string", length=255) */ private $name; /**** * Set name * * @param string $name * @return Attribute */ public function setName($name) { $this->name = $name; return $this; } /**** * Get name * * @return string */ public function getName() { return $this->name; } /**** * @var \DateTime $created * * @Gedmo\Timestampable(on="create") * @ORM\Column(type="datetime") */ private $created; /**** * @return \DateTime */ public function getCreated() { return $this->created; } } ``` And ofcourse invalidates the entity because it now has two methods of the getCreated and two of **private $created**
Author
Owner

@doctrinebot commented on GitHub (Dec 1, 2014):

Comment created by wilgert:

Unfortunately I am also suffering from this bug. Is there anything I can do to help resolve it?

@doctrinebot commented on GitHub (Dec 1, 2014): Comment created by wilgert: Unfortunately I am also suffering from this bug. Is there anything I can do to help resolve it?
Author
Owner

@doctrinebot commented on GitHub (Mar 24, 2015):

Comment created by ludwig.ruderstaller:

Same here - i think an easy fix would be to introduce an additional parameter, which if set, ignores all traits.

@doctrinebot commented on GitHub (Mar 24, 2015): Comment created by ludwig.ruderstaller: Same here - i think an easy fix would be to introduce an additional parameter, which if set, ignores all traits.
Author
Owner

@henrypenny commented on GitHub (Mar 7, 2016):

I've been successfully using traits with doctrine and symfony 2 in this way without the problem mentioned. ( I have not tried to use Doctrine in this way outside of Symfony though )

The only difference I can see is that I use the Entity annotation on the trait:

use Doctrine\ORM\Mapping as ORM;

/**
 * Locatable
 *
 * @ORM\Entity
 */
trait Locatable
{

    /**
     * @ORM\Column(name="google_location", type="text", nullable=true)
     */
    private $googleLocation;

    /**
     * @ORM\Column(name="coordinates", type="string", nullable=true)
     */
    private $coordinates;

    /**
     * Set googleLocation
     *
     * @param string $googleLocation
     *
     * @return Locatable
     */
    public function setGoogleLocation($googleLocation)
    {
        $this->googleLocation = $googleLocation;

        return $this;
    }

    /**
     * Get googleLocation
     *
     * @return string
     */
    public function getGoogleLocation()
    {
        return $this->googleLocation;
    }

    /**
     * Set coordinates
     *
     * @param string $coordinates
     *
     * @return Locatable
     */
    public function setCoordinates($coordinates)
    {
        $this->coordinates = $coordinates;

        return $this;
    }

    /**
     * Get coordinates
     *
     * @return string
     */
    public function getCoordinates()
    {
        return $this->coordinates;
    }
}

Process:

  1. Change trait to class
  2. Use app/console doctrine:generate:entities AppBundle:Locatable
  3. Change class back to trait
  4. Add trait to the useing class e.g. AppBundle:Region
  5. app/console doctrine:generate:entities AppBundle:Region makes no changes to Region
  6. app/console doctrine:migrations:diff etc... produces the new fields on any classes using the trait

The only complaint I have is that I can't use doctrine:generate:entities on the trait without changing the trait keyword temporarily.

@henrypenny commented on GitHub (Mar 7, 2016): I've been successfully using traits with doctrine and symfony 2 in this way without the problem mentioned. ( I have not tried to use Doctrine in this way outside of Symfony though ) The only difference I can see is that I use the Entity annotation on the trait: ``` use Doctrine\ORM\Mapping as ORM; /** * Locatable * * @ORM\Entity */ trait Locatable { /** * @ORM\Column(name="google_location", type="text", nullable=true) */ private $googleLocation; /** * @ORM\Column(name="coordinates", type="string", nullable=true) */ private $coordinates; /** * Set googleLocation * * @param string $googleLocation * * @return Locatable */ public function setGoogleLocation($googleLocation) { $this->googleLocation = $googleLocation; return $this; } /** * Get googleLocation * * @return string */ public function getGoogleLocation() { return $this->googleLocation; } /** * Set coordinates * * @param string $coordinates * * @return Locatable */ public function setCoordinates($coordinates) { $this->coordinates = $coordinates; return $this; } /** * Get coordinates * * @return string */ public function getCoordinates() { return $this->coordinates; } } ``` Process: 1. Change `trait` to `class` 2. Use `app/console doctrine:generate:entities AppBundle:Locatable` 3. Change `class` back to `trait` 4. Add trait to the `use`ing class e.g. AppBundle:Region 5. `app/console doctrine:generate:entities AppBundle:Region` makes no changes to `Region` 6. `app/console doctrine:migrations:diff` etc... produces the new fields on any classes using the `trait` The only complaint I have is that I can't use `doctrine:generate:entities` on the trait without changing the `trait` keyword temporarily.
Author
Owner

@Ocramius commented on GitHub (Mar 7, 2016):

Traits are well beyond the scope of the entity generator.
There is no need to generate entities on existing classes anyway (if you do it just for the getter/setters, there are other development tools for that).

Closing as Won't Fix

@Ocramius commented on GitHub (Mar 7, 2016): Traits are well beyond the scope of the entity generator. There is no need to generate entities on existing classes anyway (if you do it just for the getter/setters, there are other development tools for that). Closing as `Won't Fix`
Author
Owner

@henrypenny commented on GitHub (Mar 7, 2016):

Can you reopen this? My current workflow is extremely powerful and I'm using it all the time.
I can't imagine that allowing the generator to accept a trait would be a big change.

@henrypenny commented on GitHub (Mar 7, 2016): Can you reopen this? My current workflow is extremely powerful and I'm using it all the time. I can't imagine that allowing the generator to accept a trait would be a big change.
Author
Owner

@Ocramius commented on GitHub (Mar 7, 2016):

@henrypenny generating getters/setters is out of the scope of the entity generator in general: the fact that symfony taught you that has nothing to do with what it was intended for (importing legacy DB schemas as entities)

@Ocramius commented on GitHub (Mar 7, 2016): @henrypenny generating getters/setters is out of the scope of the entity generator in general: the fact that symfony taught you that has nothing to do with what it was intended for (importing legacy DB schemas as entities)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2297