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?