Doctrine2重复来自trait的属性和方法

我有一个实体,我想在其中使用特征“TimestampableEntity”来映射一些属性:

namespace WbudowiePortalBundleEntity;

use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
use WbudowiePortalBundleTraitsTimestampableEntity;

/**
 * Category
 * 
 * @GedmoTree(type="materializedPath")
 * @GedmoSoftDeleteable(fieldName="deletedAt", timeAware=false)
 * @ORMTable(name="categories")
 * @ORMEntity(repositoryClass="CategoryRepository")
 */
class Category {

    use TimestampableEntity;

    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

这是我的TimestampableEntity特性:

namespace WbudowiePortalBundleTraits;

use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;

trait TimestampableEntity {

    /**
     * @var DateTime
     *
     * @GedmoTimestampable(on="create")
     * @ORMColumn(name="created_at", type="datetime")
     */
    private $createdAt;

    /**
     * @var DateTime
     *
     * @GedmoTimestampable(on="update")
     * @ORMColumn(name="edited_at", type="datetime", nullable=true)
     */
    private $editedAt;

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

    /**
     * @var boolean
     *
     * @ORMColumn(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 WbudowiePortalBundle对我的实体添加了以下代码:

/**
 * @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


这里

这就是你可能需要的,以实现你的目标。

据此,这是你无能为力的事情。 只是不要使用生成实体:)


我已经取得了成功。 试试这些步骤:

首先将实体注释添加到你的特质中:

/**
* @ORMEntity 
*/
trait TimestampableEntity

暂时将其更改为一个class

/**
* @ORMEntity 
*/
class TimestampableEntity

运行generate:entities你的特质的generate:entities

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

TimestampableEntity更改回特质:

/**
* @ORMEntity 
*/
trait TimestampableEntity

将特征添加到Category

class Category {

    use TimestampableEntity;

如果您现在使用:

app/console doctrine:generate:entities WbudowiePortalBundle:Category

你不应该看到重复。 我不确定,但我相信这是有效的,因为你的特质的getter和setters现在将与可能在Category创建的特征相同,因此它们不会被替换。 对getter和设置进行手工编码可能会导致差异,导致Doctrine认为Category需要新代码。

链接地址: http://www.djcxy.com/p/69381.html

上一篇: Doctrine2 duplicate properties and methods from trait

下一篇: Symfony2 Fatal error in Users Entity Provider