[英]primary/foreign key relationship in doctrine
在我的mysql數據庫上,我有兩個表:“ User”和“ UserProfile”。 表“ UserProfile”具有一個名為“ user_id”的外鍵列,該外鍵列鏈接至“用戶”表的“ id”列。 現在,當我使用doctrine從數據庫表中生成所有實體類時,創建的“ UserProfile”類包含一個名為“ user”的屬性(屬於“ User”類型),並且不包含任何名為“ user_id”的屬性。 可以嗎?
現在,如果要查找用戶的個人資料,給定user_id,我需要編寫如下內容:
$user_profile_repo = $this->em->getRepository("UserProfile");
$user_profile = $user_profile_repo->findOneBy(array("user_id"=>$id));
但是由於生成的實體類不包含“ user_id”屬性,因此上述代碼將無法工作。 現在,我需要知道如何進行調整以使上述代碼正常工作嗎? 謝謝。
數據庫中表/列的實際名稱並不是很重要。 您可以在評論中進行設置。
應該是這樣的:
/**
* models\User
* @Table(name="User")
* @Entity
*/
class User
{
/**
* @Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @Column(name="name", type="string", length=50, precision=0, scale=0, nullable=false, unique=false)
*/
private $name;
/**
* @OneToOne(targetEntity="models\UserProfile", mappedBy="user")
*/
private $profile;
}
/**
* models\UserProfile
* @Table(name="UserProfile")
* @Entity
*/
class UserProfile
{
/**
* @OneToOne(targetEntity="models\User", inversedBy("profile"))
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
}
在這種情況下,用戶具有列ID,而用戶配置文件具有user_id
生成后,您應該在User中獲取方法getProfile(),在UserProfile中您應該獲取getUser()
它與5.7 oneToOne雙向相似: http : //www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.