Duplication of generated methods / attributes with Traits #297

Closed
khepin opened this Issue May 1, 2012 · 7 comments

5 participants

@khepin

When a class is set to use a given trait as for example the one provided by
DoctrineExtensions,
the properties and methods defined in the trait file are re-created in the file
defining the class even though they already exist in the Trait definition.

For example here's what I get for a comment thread class in a SF project if I use
the doctrine:mongodb:generate:documents command:

<?php
namespace Xxx\CommentBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ODM\Document
 */
class Thread
{
    use \Gedmo\Timestampable\Traits\TimestampableDocument;

    /**
     * @ODM\Id
     */
    private $id;

    /**
     * @ODM\ReferenceMany(targetDocument="Comment")
     */
    private $comments;

    public function __construct()
    {
        $this->comments = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return id $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add comments
     *
     * @param Xxx\CommentBundle\Document\Comment $comments
     */
    public function addComments(\Xxx\CommentBundle\Document\Comment $comments)
    {
        $this->comments[] = $comments;
    }

    /**
     * Get comments
     *
     * @return Doctrine\Common\Collections\Collection $comments
     */
    public function getComments()
    {
        return $this->comments;
    }
    /**
     * @var date $createdAt
     */
    protected $createdAt;

    /**
     * @var date $updatedAt
     */
    protected $updatedAt;


    /**
     * Set createdAt
     *
     * @param date $createdAt
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;
    }

    /**
     * Get createdAt
     *
     * @return date $createdAt
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set updatedAt
     *
     * @param date $updatedAt
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;
    }

    /**
     * Get updatedAt
     *
     * @return date $updatedAt
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }
}
@jmikola
Doctrine member

Can you share the mapping (presumably it was XML or YML) that resulted in this generated code? I'd like to try and reproduce it.

Although doctrine:mongodb:generate:documents is in DoctrineMongoDBBundle, all of the action happens in ODM's DocumentGenerator class, so you were right to open the issue here; however, I didn't find anything in the class that deals with traits (even conditionally). I'm not sure how the use statement is being printed, but I presume the createdAt and updatedAt getters and setters are simply printed because the fields exist in the document mapping.

@khepin

The mapping was with annotations. A sample mapping is there: https://github.com/khepin/DoctrineMongoDuplicateGetSet/tree/master/src/Acme/DemoBundle/Document

After getting the project file if you run composer and then doctrine:mongodb:generate:documents AcmeDemoBundle, the 'Article.php' file will contain a getCreatedAt and a setCreatedAt method even though they already exist in the trait.

@jmikola jmikola was assigned Jun 15, 2012
@jmikola
Doctrine member

I just installed PHP 5.4 on my local machine, so I should be able to reproduce this soon and work on a fix.

@stof
Doctrine member

@jmikola I know that the ORM fixed some bugs about inherited methods from parent classes too a while ago. Not sure if it was applied to the ODM DocumentGenerator. It may be related

@dorongutman

I encounter this issue as well - proxy generation ignores traits, and therefor my "getId" method didn't returned the id lazy, but rather filled the entire referenced document' properties.

Any updates on the fix of it ?

@jmikola jmikola added this to the 1.0.0-BETA12 milestone Sep 5, 2014
@jmikola jmikola added the bug label Sep 5, 2014
@malarzm
Doctrine member

Just to cross reference, proxy issue is reported in doctrine/common#337

As for the original generator issue I've found two related PRs in ORM:

I'll look into porting this solution into ODM

@jmikola jmikola modified the milestone: 1.0.0-BETA12, 1.0.0-BETA13 Feb 24, 2015
@malarzm malarzm added the has PR label Mar 6, 2015
@malarzm
Doctrine member

#988 has been merged in 32912d1

@malarzm malarzm closed this Mar 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment