gpt4 book ai didi

php - Doctrine2 从特征中复制属性和方法

转载 作者:可可西里 更新时间:2023-10-31 23:40:22 24 4
gpt4 key购买 nike

我有一个实体,我想在其中使用特征“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;
}

在那之后,当我尝试做任何事情时,我都会收到这个错误,所以我必须删除生成的代码:

Runtime Notice: Wbudowie\PortalBundle\Entity\Category and Wbudowie\PortalBundle\Traits\TimestampableEntity define the same property ($createdAt) in the composition of Wbudowie\PortalBundle\Entity\Category. This might be incompatible, to improve maintainability consider using accessor methods in traits instead. Class was composed in /mnt/DANE/projekty/wbudowie/src/Wbudowie/PortalBundle/Entity/Category.php line 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

最佳答案

Here

这就是您实现目标可能需要的全部内容。

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

关于php - Doctrine2 从特征中复制属性和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24800349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com