大家好,感謝您的幫助!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」爲空?