0

Hi everyone and thanks for help in advance!

I'm a newbe in a Symfony2 framework and I faced a question:

How to create bidirectional relationships from existing database?

At first I created database for my project and than I mapped it to the yml files; Simply, DB looks like this:

Table 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

Table 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

If I'm right, "user" have Many-to-One relationship to the "customer"; and "user" is an owning side, "customer" is an inverse side;

Then I run these commands:

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

And got the result:

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

And conforming entities by run command:

php app/console doctrine:generate:entities ShadowTestBundle

Entities reflects correctly the yml-files;

But both yml-files and entities use only unidirectional links; is it possible to generate bi-directional links, or I must write it manually? As far as I see, it should look like that:

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

And I also have a little subquestion: Why on the owning side (User), which generated by Doctrine, field "inversedBy" is null?

2 Answers2

2

The imported mappings generated by app/console doctrine:mapping:import are not always correctly reflecting the complete database structure i.e. when it comes to non-primary keys.

The mapping's inversedBy attribute is set to null because doctrine can't guess your owning-side-entity's desired $property name for storing the inverse-side entity from a database - therefore the mapping is generated without inversedBy set...

... which results in the auto-generated/expected property-name being the camelCase representation of the targetEntity as default / convention.

If you are not importing hundreds of tables i would recommend targeting these little corrections by hand and not linger over the import command.

Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
0

it's worth checking the documentation.

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

From the document,

Reverse Engineering is a one-time process that can get you started with a project. Converting an existing database schema into mapping files only detects about 70-80% of the necessary mapping information. Additionally the detection from an existing database cannot detect inverse associations, inheritance types, entities with foreign keys as primary keys and many of the semantical operations on associations such as cascade.

Hope this helps. Cheers!

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54