[英]Primary key and foreign key at the same time with doctrine 2
我有兩張桌子:
表ID為主鍵的表A.
表B , id為主鍵和外鍵
簡短說明:
我需要在表B中有一個主鍵,它也是一個指向表A主鍵的外鍵。
誰能解釋我如何通過Doctrine 2中的注釋來映射這個?
注意:
我試過了這個:
class A
{
/**
* @var bigint $id
*
* @Column(name="id", type="bigint", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $a_id;
...
和B表:
class B
{
/**
* @var bigint $id
* @Id
* @OneToOne(targetEntity="A", fetch="LAZY")
* @JoinColumn(name="id", referencedColumnName="id")
*/
private $b_id;
...
但它給了我這個錯誤:
未捕獲的異常'Doctrine \\ ORM \\ Mapping \\ MappingException',帶有消息'沒有為實體'B'指定標識符/主鍵。 每個實體都必須有一個標識符/主鍵。 在/var/www/agr-reg-php/Doctrine/ORM/Mapping/MappingException.php:37堆棧跟蹤:
注意:我不能擁有復合主鍵。
從Doctrine 2.1開始,這是可能的:
通過外部實體或派生實體的身份 :您現在可以使用外鍵作為實體的標識符。 這轉換為在@ManyToOne或@OneToOne關聯上使用@Id。 您可以在本教程中閱讀此功能 。
我可以解決這個問題,創建一個與外部字段同名的pk字段
class B
{
/**
* @var bigint $a_id
* @Id @Column(name="a_id", type="bigint", nullable="false")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $a_id;
/**
* @OneToOne(targetEntity="A", fetch="LAZY")
* @JoinColumn(name="id", referencedColumnName="id")
*/
private $a;
.
.
.
/**
* @var A
*
* @ORM\OneToOne(targetEntity="A")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="a_id", referencedColumnName="id", unique=true)
* })
*/
private $a;
//...
那么這是插入的其他時間解決方案:
/**
* @Entity
* @Table(name="types")
*/
class Type {
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
private $id;
/**
* @OneToMany(targetEntity="type\models\Language", mappedBy="type")
*/
private $languages;
/**
* @Column(type="string", length = 45,nullable = true)
*/
private $category;
/**
* @Column(type="string", length = 1)
*/
private $status;
/**
* @Column(type="string", length = 45)
*/
private $name;
/**
* @Column(type="datetime", nullable = true)
*/
private $datedeleted;
/**
* @Column(type="datetime", nullable = true)
*/
private $datemodificated;
/**
* @Column(type="datetime")
*/
private $dateregistered;
public function __construct() {
$this->languages = new \Doctrine\Common\Collections\ArrayCollection;
$this->status = 1;
$this->dateregistered = new \DateTime("now");
}
public function id() {
return $this->id;
}
public function category($value = NULL) {
if (is_null($value))
return $this->category;
else
$this->category = $value;
}
public function name($value = NULL) {
if (is_null($value))
return $this->name;
else
$this->name = $value;
}
public function status($value = NULL) {
if (is_null($value))
return $this->status;
else
$this->status = $value;
}
public function datedeleted($value = NULL) {
if (is_null($value))
return $this->datedeleted;
else
$this->datedeleted = $value;
}
public function datemodificated($value = NULL) {
if (is_null($value))
return $this->datemodificated;
else
$this->datemodificated = $value;
}
public function dateregistered($value = NULL) {
if (is_null($value))
return $this->dateregistered;
else
$this->dateregistered = $value;
}
}
/**
* @Entity
* @Table(name="languages")
*/
class Language {
/**
* @Id
* @Column(name="type_id", type="integer", nullable="false")
*/
private $language_id;
/**
* @Id
* @ManyToOne(targetEntity="type\models\Type",inversedBy="languages")
* @JoinColumn(name="type_id", referencedColumnName="id")
*/
private $type;
/**
* @Column(type="string", length = 100, nullable = true)
*/
private $description;
/**
* @Id
* @Column(type="string", length = 20)
*/
private $language;
public function language_id($value) {
$this->language_id = $value;
}
public function description($value = NULL) {
if (is_null($value))
return $this->description;
else
$this->description = $value;
}
public function language($value = NULL) {
if (is_null($value))
return $this->language;
else
$this->language = $value;
}
public function type($value = NULL) {
if (is_null($value))
return $this->type;
else
$this->type = $value;
}
}
$language = new Language;
$xtype_id = $this->em->find("Type",1);
$language->language_id($xtype_id->id());
$language->type($xtype_id);
$language->description($xdescription);
$language->language($xlanguage);
$this->em->persist($this);
$this->em->flush();
此插入在類中使用外鍵和主鍵的語言
我有同樣的任務,實驗中找到了這個解決方案:
class A {
/**
* @Id @Column(type="integer", nullable=false)
* @GeneratedValue
*/
protected $id;
/**
* @OneToOne(targetEntity="B", inversedBy="a", orphanRemoval=true, cascade={"persist", "remove"})
* @JoinColumn(name="id", referencedColumnName="id")
*/
protected $b;
}
class B {
/**
* @OneToOne(targetEntity="A", mappedBy="b" )
*/
protected $user;
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue
*/
protected $id;
}
最后,我通過在實體類中為真實表中的同一列指定兩個字段來解決我的問題。 更改僅在B類中進行(查看A類的問題):
class B
{
/**
* @var bigint $id
* @Id @Column(name="id", type="bigint", nullable="false")
*/
private $b_id;
/**
* @OneToOne(targetEntity="A", fetch="LAZY")
* @JoinColumn(name="id", referencedColumnName="id")
*/
private $a;
...
事實上,我所做的就是在我的實體中為相同的主鍵和外鍵寫兩個字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.