Doctrine 提供了指定生成字段的可见性的方法。受保护或私有。默认为私有。
问题是调用 Doctrine 的 Symfony 命令无法改变这一点。
创建您自己的标准 Symfony 命令的子类将允许您更好地控制生成过程。这可能会帮助你。
namespace Foo\Bundle\FooBundle\Command;
use Doctrine\Bundle\DoctrineBundle\Command as DC;
use Doctrine\ORM\Tools\EntityGenerator;
class GenerateEntitiesDoctrineCommand extends DC\GenerateEntitiesDoctrineCommand
{
protected function configure()
{
parent::configure();
$this->setName('foo:generate:entities');
}
/**
* get a doctrine entity generator
*
* @return EntityGenerator
*/
protected function getEntityGenerator()
{
$entityGenerator = new EntityGenerator();
$entityGenerator->setGenerateAnnotations(true);
$entityGenerator->setGenerateStubMethods(true);
$entityGenerator->setRegenerateEntityIfExists(false);
$entityGenerator->setUpdateEntityIfExists(true);
$entityGenerator->setNumSpaces(4);
$entityGenerator->setAnnotationPrefix('ORM\\');
$entityGenerator->setFieldVisibility($entityGenerator::FIELD_VISIBLE_PROTECTED);
return $entityGenerator;
}
}
这有两件事。它将属性可见性设置为受保护。这可以防止 php 错误。
$entityGenerator->setFieldVisibility($entityGenerator::FIELD_VISIBLE_PROTECTED);
它还将注释从映射的超类复制到实体类中。
$entityGenerator->setGenerateAnnotations(true);
这是一些示例代码,其中属性从基类继承,并且它们的可见性和注释正确复制到继承类中
/** @ORM\MappedSuperclass */
class DataSuper {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Campaign", inversedBy="data")
* @ORM\JoinColumn(name="campaign_id", referencedColumnName="id")
* @Exclude
*/
protected $campaign;
/**
* @ORM\Column(type="text", nullable=true, name="data")
*/
protected $data;
/**
* @ORM\Column(type="datetime")
*/
protected $createdDate;
}
/**
* @ORM\Entity(repositoryClass="Foo\Bundle\FooBundle\Entity\DataRepository")
* @ORM\Table(name="data")
* @ExclusionPolicy("none")
*/
class Data extends DataSuper
{
}
生成后的 Data 类如下所示:
class Data extends DataSuper
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="data", type="text", precision=0, scale=0, nullable=true, unique=false)
*/
protected $data;
/**
* @var \DateTime
*
* @ORM\Column(name="createdDate", type="datetime", precision=0, scale=0, nullable=false, unique=false)
*/
protected $createdDate;
/**
* @var \Foo\Bundle\FooBundle\Entity\Campaign
*
* @ORM\ManyToOne(targetEntity="Foo\Bundle\FooBundle\Entity\Campaign", inversedBy="data")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="campaign_id", referencedColumnName="id", nullable=true)
* })
*/
protected $campaign;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set data
*
* @param string $data
* @return Data
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Get data
*
* @return string
*/
public function getData()
{
return $this->data;
}
/**
* Set createdDate
*
* @param \DateTime $createdDate
* @return Data
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* @return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set campaign
*
* @param \Foo\Bundle\FooBundle\Entity\Campaign $campaign
* @return Data
*/
public function setCampaign(\Foo\Bundle\FooBundle\Entity\Campaign $campaign = null)
{
$this->campaign = $campaign;
return $this;
}
/**
* Get campaign
*
* @return \Foo\Bundle\FooBundle\Entity\Campaign
*/
public function getCampaign()
{
return $this->campaign;
}
}
一旦你这样做,表结构是正确的:
php app/console doctrine:schema:update --force