我正在使用带有ZF2的Doctrine 2。
我有一个具有以下主键字段的网站实体。
/**
* @var string
* @ORM\Column(name="site_id", type="string", length=10, nullable=false)
* @ORM\Id
*/
private $siteId;
以下索引的
* @ORM\Table(name="sites", indexes={
* @ORM\Index(name="PRIMARY", columns={"site_id"}),
* @ORM\Index(name="country_id", columns={"country_id"}),
* @ORM\Index(name="timezone_id", columns={"timezone_id"}),
* @ORM\Index(name="vat_rate_id", columns={"vat_rate_id"}),
* @ORM\Index(name="site_mode_id", columns={"site_mode_id"}),
* @ORM\Index(name="created_by_user_id", columns={"created_by_user_id"}),
* })
当我从命令行运行php ./vendor/doctrine/doctrine-module/bin/doctrine-module orm:validate-schema
时,我收到以下错误消息。
[Doctrine\DBAL\Schema\SchemaException]
An index with name 'primary' was already defined on table 'sites'.
但它也会报告The mapping files are correct.
有谁知道为什么会产生这个错误?
非常感谢提前。
修改
要求的完整实体
/**
* Sites Entity
*
* @author Garry Childs
*
* @ORM\Table(name="sites", indexes={
* @ORM\Index(name="country_id", columns={"country_id"}),
* @ORM\Index(name="timezone_id", columns={"timezone_id"}),
* @ORM\Index(name="vat_rate_id", columns={"vat_rate_id"}),
* @ORM\Index(name="site_mode_id", columns={"site_mode_id"}),
* @ORM\Index(name="created_by_user_id", columns={"created_by_user_id"}),
* })
* @ORM\Entity(repositoryClass="Application\Entity\Repository\SitesRepository")
* @ORM\HasLifecycleCallbacks
*/
class Sites extends AbstractEntity
{
/**
* @var string
* @ORM\Column(name="site_id", type="string", length=10, nullable=false)
* @ORM\Id
*/
private $siteId;
/**
* @var string
*
* @ORM\Column(name="domain_name", type="string", length=255, nullable=false)
*/
private $domainName;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=30, nullable=false)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="email_address", type="string", length=254, nullable=false)
*/
private $emailAddress;
/**
* @var string
*
* @ORM\Column(name="layout", type="string", length=30, nullable=true)
*/
private $layout;
/**
* @var string
*
* @ORM\Column(name="homepage", type="string", length=30, nullable=true)
*/
private $homepage;
/**
* @var string
*
* @ORM\Column(name="bookmark_icon", type="string", length=20, nullable=false)
*/
private $bookmarkIcon = 'bookmark.png';
/**
* @var string
*
* @ORM\Column(name="address", type="string", length=200, nullable=false)
*/
private $address;
/**
* @var string
*
* @ORM\Column(name="town", type="string", length=30, nullable=false)
*/
private $town;
/**
* @var string
*
* @ORM\Column(name="county", type="string", length=30, nullable=false)
*/
private $county;
/**
* @var \Application\Entity\Countries
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Countries")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="country_id", referencedColumnName="country_id")
* })
*/
private $country;
/**
* @var string
*
* @ORM\Column(name="post_code", type="string", length=7, nullable=false)
*/
private $postCode;
/**
* @var \Application\Entity\Timezones
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Timezones")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="timezone_id", referencedColumnName="timezone_id")
* })
*/
private $timezone;
/**
* @var string
*
* @ORM\Column(name="locale", type="string", length=5, nullable=false)
*/
private $locale;
/**
* @var string
*
* @ORM\Column(name="currency_code", type="string", length=3, nullable=false)
*/
private $currencyCode;
/**
* @var string
*
* @ORM\Column(name="vat_number", type="string", length=10, nullable=true)
*/
private $vatNumber;
/**
* @var \Application\Entity\VatRates
*
* @ORM\ManyToOne(targetEntity="Application\Entity\VatRates", inversedBy="sites")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="vat_rate_id", referencedColumnName="vat_rate_id")
* })
*/
private $vatRate;
/**
* @var \DateTime
*
* @ORM\Column(name="date_created", type="datetime")
*/
private $dateCreated;
/**
* @var \DateTime
*
* @ORM\Column(name="date_modified", type="datetime")
*/
private $dateModified;
/**
* @var \Application\Entity\Users
*
* @ORM\ManyToOne(targetEntity="Application\Entity\Users")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="created_by_user_id", referencedColumnName="user_id")
* })
*/
private $createdBy;
/**
* @var Doctrine\ORM\PersistentCollection
*
* @ORM\OneToMany(targetEntity="Application\Entity\Categories", mappedBy="site")
*/
private $categories;
/**
* @var \Doctrine\ORM\PersistentCollection
*
* @ORM\OneToMany(targetEntity="Application\Entity\SiteCountries", cascade="persist", mappedBy="site")
*/
private $siteCountries;
/**
* @var \Doctrine\ORM\PersistentCollection
*
* @ORM\OneToMany(targetEntity="Application\Entity\SiteShippingMethods", cascade="persist", mappedBy="site")
*/
private $shippingMethods;
/**
* @var \Doctrine\ORM\PersistentCollection
*
* @ORM\OneToMany(targetEntity="Application\Entity\SitePaymentMethods", cascade="persist", mappedBy="site")
*/
private $sitePaymentMethods;
/**
* @var \Application\Entity\SiteModes
*
* @ORM\ManyToOne(targetEntity="Application\Entity\SiteModes")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="site_mode_id", referencedColumnName="site_mode_id")
* })
*/
private $siteMode;
/**
*
* @var integer
* @ORM\Column(name="payment_days", type="integer", nullable=false)
*/
private $paymentDays;
/**
*
* @var integer
* @ORM\Column(name="products_per_page", type="integer", nullable=false)
*/
private $productsPerPage;
/**
* @var \Doctrine\ORM\PersistentCollection
*
* @ORM\OneToMany(targetEntity="Application\Entity\Invoices", mappedBy="site")
* })
*/
private $invoices;
public function __construct()
{
$this->categories = new ArrayCollection();
$this->siteCountries = new ArrayCollection();
$this->shippingMethods = new ArrayCollection();
$this->vatRate = NULL;
$this->sitePaymentMethods = new ArrayCollection();
$this->invoices = new ArrayCollection();
$this->productsPerPage = 15;
}
.... Getters & Setters
/**
* @ORM\PrePersist
* @return \Application\Entity\Users
*/
public function prePersist()
{
$this->dateCreated = $this->getCurrentDateTime();
$this->dateModified = $this->getCurrentDateTime();
$this->currencyCode = $this->strToUpper($this->currencyCode);
$this->createdBy = $this->getAuthUser();
return $this;
}
/**
* @ORM\PreUpdate
* @return \Application\Entity\Users
*/
public function preUpdate()
{
$this->dateModified = $this->getCurrentDateTime();
$this->currencyCode = $this->strToUpper($this->currencyCode);
return $this;
}
答案 0 :(得分:0)
将site_id
注释添加到@ORM\Id
属性时,您已将$siteId
列标记为主列。没有必要添加以下行:
@ORM\Index(name="PRIMARY", columns={"site_id"}),
删除该行,您会看到site_id
列将在数据库中自动正确索引为PRIMARY
索引。
在学说文档
中阅读有关此in chapter 4.5. Identifiers / Primary Keys的更多信息