Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplication of generated methods / attributes with Traits #297

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

Duplication of generated methods / attributes with Traits #297

khepin opened this issue May 1, 2012 · 7 comments
Assignees
Milestone

Comments

@khepin
Copy link
Contributor

khepin commented May 1, 2012

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
Copy link
Member

jmikola commented May 17, 2012

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
Copy link
Contributor Author

khepin commented May 23, 2012

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.

@ghost ghost assigned jmikola Jun 15, 2012
@jmikola
Copy link
Member

jmikola commented Jun 15, 2012

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
Copy link
Member

stof commented Jul 20, 2012

@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
Copy link

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
Copy link
Member

malarzm commented Dec 4, 2014

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

@malarzm
Copy link
Member

malarzm commented Mar 26, 2015

#988 has been merged in 32912d1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants