2014-07-17 24 views
4

屬性和方法,我有一個實體,在這裏我想用特質「TimestampableEntity」映射某些屬性:Doctrine2複製從特質

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 \實體\目錄和Wbudowie \ PortalBundle \特徵\ TimestampableEntity定義相同屬性($ createdAt)在Wbudowie \ PortalBundle \ Entity \ Category的組合中。這可能是不兼容的,爲了提高可維護性,請考慮在特徵中使用訪問器方法。類/mnt/DANE/projekty/wbudowie/src/Wbudowie/PortalBundle/Entity/Category.php線607

什麼是錯的組成?我使用PHP 5.5.9,學說2.4 *和Symfony的2.5 *


編輯:。 它,學說錯誤。它是固定的,但不在2.4.x版本中。

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

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

+0

請寫下你正在努力實現的目標 –

+0

我不想複製從特質到實體的所有內容,因爲我想讓我的特質擁有一切。 – cyklista

+0

當您使用特質時,此修補程序修復生成實體: http://pastebin.com/bQppqbg5 – cyklista

回答

1

Here

這就是你將可能需要實現自己的目標。

並根據this,這是你不能做任何事情。只是不要使用生成實體:)

+1

第二個鏈接已損壞。這就是爲什麼SO政策是將外包數據的核心信息本身帶入答案的原因。 –

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中創建的吸附劑和吸附劑相同,因此它們不會被替換。對吸氣劑和設置進行手工編碼可能會導致差異,導致Doctrine認爲Category需要新代碼。