PHP

Setting Doctrine Object From ID

In a recent Symfony project I was using Doctrine as the database interface.

Doctrine is a set of PHP class libraries that are focused on database storage and database management. The main projects with Doctrine are the ORM (Object Relational Manager) and DBAL (Database Abstraction Layer). Using these two projects Doctrine allows you to build up database entities in PHP code that will represent your application's database structure.

This means that when working with data in Doctrine you are working with PHP objects. When you have one-to-many relationships in your database design you will need to link the one entity (database table) to another entity by setting a property on the PHP class entity to another entity.

$a = new EntityA();
$b = new EntityB();

$a->setB($b);

$em->persist($a);
$em->flush();

The above code is an example of how you will create a new record of entityA with a relationship to a new entityB. If you want to create a relationship link with an existing record in EntityB you need to find that record, populate a EntityB object and pass that into the setter on entity A.

$a = new EntityA();
$b = $em->getRepository('EntityB')->find(1);

$a->setB($b);

$em->persist($a);
$em->flush();

The above code will create a new EntityA which has link to EntityB on row 1.

This is a very quick description of one-to-many relationships with Doctrine. The problem I had in a recent project is that I needed to create a new entity which had relationships with a lot of other entities. I only had the ID of the record I needed in the other table so to create the new Entity with the relationships I would have to do a number of lookups to get the relationship, as you can see from the example below.

$a = new EntityA();
$b = $em->getRepository('EntityB')->find(1);
$c = $em->getRepository('EntityC')->find(1);
$d = $em->getRepository('EntityD')->find(1);
$e = $em->getRepository('EntityE')->find(1);
$f = $em->getRepository('EntityF')->find(1);
$g = $em->getRepository('EntityG')->find(1);
$h = $em->getRepository('EntityH')->find(1);
$i = $em->getRepository('EntityI')->find(1);
$j = $em->getRepository('EntityJ')->find(1);
$k = $em->getRepository('EntityK')->find(1);

$a->setB($b);
$a->setC($c);
$a->setD($d);
$a->setE($e);
$a->setF($f);
$a->setG($g);
$a->setH($h);
$a->setI($i);
$a->setJ($j);
$a->setK($k);

$em->persist($a);
$em->flush();

The problem with this is that I have to perform a number of queries to find the related Entity.

If all you want to do is find an object based off the ID, in a normal database interface you will just insert the EntityA record with the IDs of the other Entities. In doctrine you actually don't need to do all these finds you can use a function called getReference().

This function allows you to simply create the relationship from the ID instead of the entire object.

$a = new EntityA();

$a->setB($em->getReference('EntityB', 1));
$a->setC($em->getReference('EntityC', 1));
$a->setD($em->getReference('EntityD', 1));
$a->setE($em->getReference('EntityE', 1));
$a->setF($em->getReference('EntityF', 1));
$a->setG($em->getReference('EntityG', 1));
$a->setH($em->getReference('EntityH', 1));
$a->setI($em->getReference('EntityI', 1));
$a->setJ($em->getReference('EntityJ', 1));
$a->setK($em->getReference('EntityK', 1));

$em->persist($a);
$em->flush();
Back to top