2013-11-20 34 views
0

大家好,感謝您的幫助!symfony2學說從現有數據庫映射雙向鏈接

我在一個Symfony2的框架newbe和我面臨的一個問題:

如何從現有的數據庫雙向關係?

起初,我爲我的項目創建了數據庫,並將其映射到yml文件; 簡單,DB是這樣的:

user

CREATE TABLE user (
    `id` INT NOT NULL AUTO_INCREMENT , 
    `login` VARCHAR(255) NULL , 
    `password` VARCHAR(255) NULL , 
    `customer_id` INT NOT NULL ,  
    PRIMARY KEY (`id`) , 
    INDEX `fk_user_customer1_idx` (`customer_id` ASC) , 
    CONSTRAINT `fk_user_customer1` 
     FOREIGN KEY (`customer_id`) 
     REFERENCES `customer` (`id`) 
) ENGINE = InnoDB 

customer

CREATE TABLE IF NOT EXISTS `customer` (
    `id` INT NOT NULL AUTO_INCREMENT , 
    `surname` VARCHAR(45) NULL , 
    `name` VARCHAR(45) NULL , 
    `midname` VARCHAR(45) NULL , 
    PRIMARY KEY (`id`) 
) ENGINE = InnoDB 

如果我是正確的, 「用戶」 有許多-to-one的關係「顧客」;而「用戶」是一方,「顧客」是一個反面;

然後我運行這些命令:

php app/console doctrine:mapping:import ShadowTestBundle yml --force 

,並得到了結果:

Shadow\TestBundle\Entity\User: 
    type: entity 
    table: user 
    fields: 
     id: 
      id: true 
      type: integer 
      unsigned: false 
      nullable: false 
      generator: 
       strategy: IDENTITY 
     login: 
      type: string 
      length: 255 
      fixed: false 
      nullable: true 
     password: 
      type: string 
      length: 255 
      fixed: false 
      nullable: true 
    manyToOne: 
     customer: 
      targetEntity: Customer 
      cascade: { } 
      mappedBy: null 
      inversedBy: null 
      joinColumns: 
       customer_id: 
        referencedColumnName: id 
      orphanRemoval: false 
    lifecycleCallbacks: { } 


Shadow\TestBundle\Entity\Customer: 
    type: entity 
    table: customer 
    fields: 
     id: 
      id: true 
      type: integer 
      unsigned: false 
      nullable: false 
      generator: 
       strategy: IDENTITY 
     surname: 
      type: string 
      length: 45 
      fixed: false 
      nullable: true 
     name: 
      type: string 
      length: 45 
      fixed: false 
      nullable: true 
     midname: 
      type: string 
      length: 45 
      fixed: false 
      nullable: true 
    lifecycleCallbacks: { } 

,符合通過運行命令實體:

php app/console doctrine:generate:entities ShadowTestBundle 

實體反映正確的YML-文件;

但是,yml文件和實體都只使用單向鏈接;是否可以生成雙向鏈接,或者我必須手動編寫它? 據我看到的,它應該是一個:

Shadow\TestBundle\Entity\Customer: 
    type: entity 
    table: customer 
    fields: 
... 
     midname: 
      type: string 
      length: 45 
      fixed: false 
      nullable: true 
     oneToMany: 
      user: 
       targetEntity: User 
       mappedBy: cart 
lifecycleCallbacks: { } 

而且我也有一點subquestion: 爲什麼在擁有方(用戶),它通過主義產生的,現場「inversedBy」爲空?

回答

2

app/console doctrine:mapping:import生成的導入映射是而不是總是正確地反映完整的數據庫結構,即涉及到非主鍵時。

映射的inversedBy屬性設置爲null因爲教義不能用於存儲從數據庫逆側實體猜你擁有方實體期望的$屬性名字 - 因此沒有inversedBy集產生的映射。 ...

...這導致自動生成/預期的屬性名稱爲camelCasetargetEntity表示爲默認/約定。

如果您沒有導入數百個表格,我建議您手動定位這些小小的更正,而不要徘徊在導入命令之上。

0

這是值得檢查的文件。

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/tools.html#reverse-engineering

從文檔,

逆向工程是一次性的過程,可以讓你開始一個項目。將現有數據庫模式轉換爲映射文件只能檢測到大約70-80%的必要映射信息。此外,來自現有數據庫的檢測無法檢測到反向關聯,繼承類型,具有外鍵作爲主鍵的實體以及關聯(如級聯)上的許多語義操作。

希望這會有所幫助。乾杯!