Paté
Paté

Reputation: 1964

Nullable manyToOne relationship in Doctrine 2

I've setup a simple mapping.

manyToOne:
    language:
      nullable: true
      targetEntity: Language
      inversedBy: questions

The entity then generated has the following method

public function setLanguage(\Sf2MCQ\CoreBundle\Entity\Language $language)
{
    $this->language = $language;
}

But now my question is how can unset a language since I can't do setLanguage(null) ?

I'm using the adminBundle and that's what is he is trying to do so I don't know If I should rewrite the generated method or If I'm missing something.

Upvotes: 3

Views: 3134

Answers (1)

peruginni
peruginni

Reputation: 81

You can unset the language if you modify your setter so that the method's argument has default null value.

public function setLanguage(\Sf2MCQ\CoreBundle\Entity\Language $language = null)
{
    $this->language = $language;
}

Then $entity->setLanguage(null) works and null will be stored after persisting the entity.

More information about typehinting allowing null value, here: http://php.net/manual/en/language.oop5.typehinting.php

Upvotes: 4

Related Questions