簡體   English   中英

與Doctrine2和Symfony2的一對一關系

[英]OneToOne relation with Doctrine2 and Symfony2

我正在嘗試在我的用戶和個人資料實體之間創建一個簡單的OneToOne關系,這是我的控制器: http : //pastie.org/5068108 ,我的用戶實體: http : //pastie.org/5068124和我的個人資料實體: http://pastie.org/5068131但是我總是會收到這個奇怪的錯誤:

注意:/opt/lampp/htdocs/Test/vendor/doctrine-dbal/lib/Doctrine/DBAL/Statement.php第98行中的數組到字符串的轉換。

當我試圖對表單進行persist()時,會出現此錯誤。

有人可以幫我嗎,我為這個錯誤而苦惱,這對我來說毫無意義。 非常感謝你。

您的用戶實體中的$ profile屬性應為:

/**
* @ORM\OneToOne(targetEntity="\Test\ProfileBundle\Entity\Profile", mappedBy="user")
* @JoinColumn(name="profile_id", referencedColumnName="id")
*/
private $profile;

並且您將必須在個人資料實體中添加以下內容:

/**
* @ORM\OneToOne(targetEntity="\Test\UserBundle\Entity\User", mappedBy="profile")
*/
private $user;

之后,應用更改:

應用/控制台原則:生成:實體測試

應用/控制台學說:模式:更新-強制

編輯:正如@ shima5指出的,我忘記添加JoinColumn批注。

在User.php中:

   /**
     * @OneToOne(targetEntity="Test\ProfileBundle\Entity\Profile")
     * @JoinColumn(name="profile_id", referencedColumnName="id")
     */
    private $profile;

這是一個單向關聯。

您不能單獨使用mappingBy =“ user”,因為它是雙向的,您需要像這樣在您的Profile中添加inversedBy =“ profile”:

   /**
     * @OneToOne(targetEntity="User", inversedBy="profile")
     * @JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

和用戶:

   /**
     * @OneToOne(targetEntity="Test\ProfileBundle\Entity\Profile", mappedBy="user")
     */
    private $profile;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM