Doctrine2重复了trait的属性和方法

时间:2014-07-17 09:56:09

标签: php symfony doctrine-orm traits

我有一个实体,我想使用trait" TimestampableEntity"用于映射某些属性:

namespace Wbudowie\PortalBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Wbudowie\PortalBundle\Traits\TimestampableEntity;

/**
 * Category
 * 
 * @Gedmo\Tree(type="materializedPath")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 * @ORM\Table(name="categories")
 * @ORM\Entity(repositoryClass="CategoryRepository")
 */
class Category {

    use TimestampableEntity;

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

这是我的TimestampableEntity特性:

namespace Wbudowie\PortalBundle\Traits;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

trait TimestampableEntity {

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

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="edited_at", type="datetime", nullable=true)
     */
    private $editedAt;

    /**
     * @var \DateTime
     * @ORM\Column(name="deleted_at",type="datetime", nullable=true)
     */
    private $deletedAt;

    /**
     * @var boolean
     *
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;

    /**
     * Sets createdAt.
     *
     * @param  \DateTime $createdAt
     * @return $this
     */
    public function setCreatedAt(\DateTime $createdAt) {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Returns createdAt.
     *
     * @return \DateTime
     */
    public function getCreatedAt() {
        return $this->createdAt;
    }

    /**
     * Sets updatedAt.
     *
     * @param  \DateTime $updatedAt
     * @return $this
     */
    public function setUpdatedAt(\DateTime $updatedAt) {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Returns updatedAt.
     *
     * @return \DateTime
     */
    public function getUpdatedAt() {
        return $this->updatedAt;
    }

    /**
     * Sets deletedAt.
     *
     * @param \Datetime|null $deletedAt
     * 
     * @return $this
     */
    public function setDeletedAt(\DateTime $deletedAt = null) {
        $this->deletedAt = $deletedAt;

        return $this;
    }

    /**
     * Returns deletedAt.
     *
     * @return \DateTime
     */
    public function getDeletedAt() {
        return $this->deletedAt;
    }

    /**
     * Is deleted?
     * 
     * @return bool
     */
    public function isDeleted() {
        return null !== $this->deletedAt;
    }

    /**
     * Set isActive
     *
     * @param boolean $isActive
     * @return $this
     */
    public function setIsActive($isActive) {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive() {
        return $this->isActive;
    }

}

当我在我的实体上运行php bin/console doctrine:generate:entities Wbudowie\PortalBundle时添加了以下代码:

/**
 * @var \DateTime
 */
private $createdAt;

/**
 * @var \DateTime
 */
private $editedAt;

/**
 * @var \DateTime
 */
private $deletedAt;

/**
 * @var boolean
 */
private $isActive;


/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return Category
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

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

/**
 * Set editedAt
 *
 * @param \DateTime $editedAt
 * @return Category
 */
public function setEditedAt($editedAt)
{
    $this->editedAt = $editedAt;

    return $this;
}

/**
 * Get editedAt
 *
 * @return \DateTime 
 */
public function getEditedAt()
{
    return $this->editedAt;
}

/**
 * Set deletedAt
 *
 * @param \DateTime $deletedAt
 * @return Category
 */
public function setDeletedAt($deletedAt)
{
    $this->deletedAt = $deletedAt;

    return $this;
}

/**
 * Get deletedAt
 *
 * @return \DateTime 
 */
public function getDeletedAt()
{
    return $this->deletedAt;
}

/**
 * Set isActive
 *
 * @param boolean $isActive
 * @return Category
 */
public function setIsActive($isActive)
{
    $this->isActive = $isActive;

    return $this;
}

/**
 * Get isActive
 *
 * @return boolean 
 */
public function getIsActive()
{
    return $this->isActive;
}

之后当我尝试做任何事情时我都会收到此错误,因此我必须删除生成的代码:

  

运行时通知:Wbudowie \ PortalBundle \ Entity \ Category和Wbudowie \ PortalBundle \ Traits \ TimestampableEntity在Wbudowie \ PortalBundle \ Entity \ Category的组合中定义相同的属性($ createdAt)。这可能是不兼容的,为了提高可维护性,请考虑在特征中使用访问器方法。课程由/mnt/DANE/projekty/wbudowie/src/Wbudowie/PortalBundle/Entity/Category.php第607行撰写

有什么问题?我使用的是PHP 5.5.9,Doctrine 2.4。*和Symfony 2.5。*


编辑: 它,学说的错误。它是固定的,但不是2.4.x版本。

https://github.com/doctrine/doctrine2/pull/632

https://github.com/doctrine/doctrine2/pull/763

2 个答案:

答案 0 :(得分:1)

Here

这就是你实现目标所需的一切。

根据this,这是你无法做任何事情的事情。只是不要使用生成实体:)

答案 1 :(得分:0)

我已经成功了。请尝试以下步骤:

首先将实体注释添加到您的特征:

/**
* @ORM\Entity 
*/
trait TimestampableEntity

暂时将其更改为class

/**
* @ORM\Entity 
*/
class TimestampableEntity

在您的特质上运行generate:entities

app/console doctrine:generate:entities WbudowiePortalBundle:Traits/TimestampableEntity

TimestampableEntity改回特质:

/**
* @ORM\Entity 
*/
trait TimestampableEntity

将特征添加到Category

class Category {

    use TimestampableEntity;

如果您现在使用:

app/console doctrine:generate:entities WbudowiePortalBundle:Category

你不应该看到重复的东西。我不确定,但我相信这是有效的,因为您的特质吸气剂和制定者现在将与Category中可能创建的那些相同,因此它们不会被替换。手动编码getter和设置可能会导致差异导致Doctrine认为Category中需要新代码。