我有一个实体
integer column (compositeId)
和一个
string column (asin)
我希望两列都作为复合键使用。 所以我在文档中查看:
但是当我尝试加载我的存储库时
$myEntity = $em->getRepository(MyEntity::class);
我收到此错误消息:
single id is not allowed on composite primary key in entity AppBundle\Entity\MyEntity
这是我的实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @Entity(repositoryClass="AppBundle\Repository\MyEntityRepository")
* @Table(name="my_entity")
*/
class MyEntity {
/**
* @Column(type="integer")
* @GeneratedValue()
*/
protected $id;
/**
* @var integer
* @Id
* @Column(type="integer", name="composite_id")
*/
protected $compositeId;
/**
* @var string
* @Id
* @Column(type="string", name="asin")
*/
protected $asin;
/**
* @var string
* @Column(type="string", name="sku")
*/
protected $sku;
/**
* @var string
* @Column(type="string", name="ean")
*/
protected $ean;
/**
* @codeCoverageIgnore
* @return mixed
*/
public function getId() {
return $this->id;
}
/**
* @param mixed $id
*
* @return MyEntity;
*/
public function setId($id) {
$this->id = $id;
return $this;
}
/**
* @codeCoverageIgnore
* @return int
*/
public function getCompositeId() {
return $this->compositeId;
}
/**
* @param int $compositeId
*
* @return MyEntity;
*/
public function setCompositeId($compositeId) {
$this->compositeId = $compositeId;
return $this;
}
/**
* @codeCoverageIgnore
* @return string
*/
public function getAsin() {
return $this->asin;
}
/**
* @param string $asin
*
* @return MyEntity;
*/
public function setAsin($asin) {
$this->asin = $asin;
return $this;
}
/**
* @codeCoverageIgnore
* @return string
*/
public function getSku() {
return $this->sku;
}
/**
* @param string $sku
*
* @return MyEntity;
*/
public function setSku($sku) {
$this->sku = $sku;
return $this;
}
/**
* @codeCoverageIgnore
* @return string
*/
public function getEan() {
return $this->ean;
}
/**
* @param string $ean
*
* @return MyEntity;
*/
public function setEan($ean) {
$this->ean = $ean;
return $this;
}
}
我做错了什么? 当我在我的 ID 表中删除 @generatedValue 时,它起作用了。 但我需要在我的 ID 列中自动生成值。 我的 ID 列中没有任何 @Id 注释,只有 @generatedValue,并且在我的两个 @Id 列中,我没有任何带有 @generatedValue 的注释,但是我得到了这个错误......
最佳答案
Doctrine 仅在 @Id
字段上支持 @GeneratedValue()
。
This annotation is optional and only has meaning when used in conjunction with @Id.
您可以使用 columnDefinition
作为解决方法,例如
@ORM\Column(type="integer", name="id", columnDefinition="INT AUTO_INCREMENT")
关于php - Symfony 3 Doctrine 组合键 : single id is not allowed on composite primary key in entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39099801/