Doctrine 2 - Disallow null value on foreign keys of ManyToOne relationships
Asked Answered
O

3

59

I have a ManyToOne relationship in one of my entities, like so:

class License {
    // ...
    /**
     * Customer who owns the license
     * 
     * @var \ISE\LicenseManagerBundle\Entity\Customer
     * @ORM\ManyToOne(targetEntity="Customer", inversedBy="licenses")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
     */
    private $customer;
    // ...
}

class Customer {
    // ...
    /**
     * Licenses that were at one point generated for the customer
     * 
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ORM\OneToMany(targetEntity="License", mappedBy="customer")
     */
    private $licenses;
    // ...
}

This generates a database schema where the "customer_id" field of the license table is allowed to be null, which is exactly what I do not want.

Here's some code where I create a record to prove that it indeed allows null values for the reference fields:

$em = $this->get('doctrine')->getEntityManager();
$license = new License();
// Set some fields - not the reference fields though
$license->setValidUntil(new \DateTime("2012-12-31"));
$license->setCreatedAt(new \DateTime());
// Persist the object
$em->persist($license);
$em->flush();

Basically, I don't want a License to be persisted without having a Customer assigned to it. Is there some annotation that needs to be set or should I just require a Customer object to be passed to my License's constructor?

The database engine I use is MySQL v5.1, and I am using Doctrine 2 in a Symfony2 application.

Odilia answered 26/5, 2011 at 9:45 Comment(3)
Do you have the code which actually creates the record? Are you using MySQL?Swoosh
@abe-petrillo I am using MySQL 5.1. I have updated the question with a code sample where I create a record.Odilia
Found it out myself. As per the doctrine annotation reference, there is a nullable option for the @Column and @JoinColumn annotations. Setting it to false leads to the behaviour I wanted.Odilia
R
85

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#annref_joincolumn

Add nullable = false to the JoinColumn annotation:

@ORM\JoinColumn(..., nullable=false)
Reins answered 26/5, 2011 at 14:23 Comment(4)
@ORM\JoinColumn(nullable=false)Belated
When using the yml format, be careful to add this property under the joinColumn and not under the name of the relation. I spent quite some time before finding it out !Onyx
Link in the answer returns 404 Not Found and need to be updatedGefell
Current documentation link doctrine-project.org/projects/doctrine-orm/en/current/reference/…Camphor
S
5

Just posting because @zim32 didn't tell where we should put the statement, so i had to make a trial and error.

Yaml:

manyToOne:
    {field}:
        targetEntity: {Entity}
        joinColumn:
            name: {field}
            nullable: false
            referencedColumnName: {id}
        cascade: ['persist']
Silsbye answered 19/12, 2017 at 18:28 Comment(0)
U
3

I couldn't find an XML example of how to do this, so I'm going to leave this snippet here in case anyone else is looking for this:

<many-to-one field="author" target-entity="User">
    <join-column name="author_id" referenced-column-name="id" nullable="false" />
</many-to-one>

The name and referenced-column-name are required, see the docs: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/xml-mapping.html#join-column-element

Uribe answered 23/7, 2018 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.