symfony2原则映射来自现有数据库的双向链接


symfony2 doctrine mapping bidirectional links from existing DB

大家好,感谢您的帮助!

我是Symfony2框架的新手,我面临一个问题:

如何从现有数据库创建双向关系?

起初,我为我的项目创建了数据库,然后将其映射到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

如果我是对的,"用户"与"客户"有多对一的关系;"用户"是拥有的一面,"客户"是相反的一面;

然后我运行以下命令:

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: {  }

我还有一个小问题:为什么在由Doctrine生成的拥有方(User),字段"invertedBy"为空?

app/console doctrine:mapping:import生成的导入映射并不始终正确反映完整的数据库结构,即当涉及到非主键时。

映射的inversedBy属性设置为null,因为条令无法猜测拥有方实体所需的$property名称,用于从数据库中存储反向方实体-因此映射是在没有inversedBy设置的情况下生成的。。。

这导致自动生成/期望的属性名称是CCD_ 7的camelCase表示作为默认/约定。

如果你没有导入数百个表,我建议你手动瞄准这些小的更正,不要在导入命令上徘徊。

值得查看文档。

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

从文件来看,

逆向工程是一个一次性的过程,可以让你开始一个项目。将现有数据库架构转换为映射文件只检测到大约70-80%的必要映射信息。此外,来自现有数据库的检测无法检测反向关联、继承类型、以外键为主键的实体以及对关联的许多语义操作(如级联)。

希望这能有所帮助。干杯