No, It is Immpossible.
NULL has a very specific meaning in SQL. It represents "no value", and you can make sure that your logic will not work even at the SQL level:
CREATE TABLE foo ( `id` INT(11) PRIMARY KEY AUTO_INCREMENT, `bar_id` INT(11) ); CREATE TABLE `bar` (`id` INT(11) PRIMARY KEY AUTO_INCREMENT); ALTER TABLE foo ADD FOREIGN KEY `bar_id_fk` (`bar_id`) REFERENCES `bar` (`id`); INSERT INTO `bar` VALUES (NULL); INSERT INTO `bar` VALUES (NULL); INSERT INTO `bar` VALUES (NULL); INSERT INTO `foo` VALUES (NULL, 1); INSERT INTO `foo` VALUES (NULL, 2); INSERT INTO `foo` VALUES (NULL, 3); INSERT INTO `foo` VALUES (NULL, 0); INSERT INTO `foo` VALUES (NULL, 4); INSERT INTO `foo` VALUES (NULL, NULL);
No, you cannot maintain a Doctrine ORM so that 0 interpreted as NULL , as this is not permitted by the RDBMS itself.
What you can do is insert โfakeโ reference records into your database, which will then act as a null object when hydrated as objects:
INSERT INTO `bar` VALUES (NULL); UPDATE `bar` SET `id` = 0 WHERE `id` = 4; INSERT INTO `foo` VALUES (NULL, 0);
In terms of essence, it looks very similar.
(Note that public properties are supported ONLY by Doctrine ORM 2.4, which are not yet released, but they make reading easier here)
foo.php:
use Doctrine\ORM\Mapping as ORM; class Foo { public $id; public $bar; }
Bar.php:
use Doctrine\ORM\Mapping as ORM; class Bar { public $id; }
And then the code to create a new Foo instance:
$nullBar = $entityManager->find('Bar', 0); $foo = new Foo(); $foo->bar = $nullBar; $em->persist($foo); $em->flush();