DDC-1825: generate entities with traits #2483

Closed
doctrinebot opened this Issue May 18, 2012 · 7 comments

4 participants

@doctrinebot

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.

@doctrinebot

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

Comment created by wilgert:

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

@doctrinebot

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.

@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@henrypenny

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.

@Ocramius Ocramius added the Won't Fix label Mar 7, 2016
@Ocramius Ocramius assigned Ocramius and unassigned beberlei Mar 7, 2016
@Ocramius
Doctrine member

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 Ocramius closed this Mar 7, 2016
@henrypenny

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.

@Ocramius
Doctrine member

@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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment