Reputation:
I am trying to get Doctrine 2 to create all columns with "not null" set. I don't want any null values in the MySQL (or whatever) database. According to the documentation, not null is default for fields created through Doctrine. However, that does not seem to extend to associations. Technically I am doing this through Symfony 2, but the problem is with how I am using the included Doctrine 2.
Here are the two sanitized entities I'm working with (in YAML):
App\Entity\Foo:
type: entity
table: foo
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: '64'
manyToOne:
bar:
targetEntity: Bar
App\Entity\Bar:
type: entity
table: bar
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: '64'
And the resulting create and alter table queries:
CREATE TABLE bar (id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(64) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE foo (id INT AUTO_INCREMENT NOT NULL,
bar_id INT DEFAULT NULL, name VARCHAR(64) NOT NULL,
INDEX IDX_8C73652189A253A (bar_id), PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE foo ADD CONSTRAINT FK_8C73652189A253A FOREIGN KEY (bar_id) REFERENCES bar(id)
I have tried searching through the documentation for answers, tried setting "nullable: false" in the manyToOne /bar entry, and creating a separate field for the "bar_id" (which came closest but resulted in entities with both setBarId and setBar methods, trading one problem for another).
Upvotes: 1
Views: 2762
Reputation: 71
From my experience there are two ways. First : Implement bidirectional association as listed in manual http://www.doctrine-project.org/docs/orm/2.1/en/reference/association-mapping.html?highlight=constraint#one-to-many-bidirectional. And than in join column add - nullable: false
Second: to add your column to section fields and there will appear ability to place all options you need to add for this join column
Upvotes: 2
Reputation: 678
Try setting nullable as a property of the joinColumn:
App\Entity\Foo:
type: entity
[...]
manyToOne:
bar:
targetEntity: Bar
joinColumn:
nullable: false
Upvotes: 1