Foreign relationships can be loaded onto an array of data objects via a method calls on ObjectMapper. Below are the steps for loading each type of relationship. On each relationship loading method there are optional parameters to define the name of the needed link column(s) / getters. If you fail to implement any of the required methods a MethodNotImplementedException will be thrown.

Many to One

  1. Loading Object must have a getter for the id of the foreign object.
  2. Loading Object must have a setter for the foreign object.
  3. Call $objectMapper->loadOne($objects, ForeignObject::class)
class MyObject extends DataObject
{
   public function getForeignObjectId()
   //..

   public function setForegnObject(ForeignObject $object)
   //..
}

Then in the code retrieving the objects:

$objects = $orm->findBy(MyObject::class, [...]);
$orm->loadOne($objects, ForeignObject::class); 

One to Many

  1. Foreign Object must have a getter for the id of the loading object.
  2. Loading Object must have a setter for the foreign objects.
  3. Call $objectMapper->loadMany($objects, ForeignObject::class)
class ForeignObject extends DataObject
{
   public function getMyObjectId()
   //..
}

class MyObject extends DataObject
{
   /**
   * @param ForeignObject[] $objects
   */
   public function setForegnObjects(array $object)
   //..
}

Then in the code retrieving the objects:

$objects = $orm->findBy(MyObject::class, [...]);
$orm->loadMany($objects, ForeignObject::class); 

Many to Many

  1. Loading Object must have a setter for the foreign objects.
  2. There must be a link table with myObjectId and foreignObjectId
  3. Call $objectMapper->loadManyToMany($objects, ForeignObject::class, 'link_table')
class MyObject extends DataObject
{
   /**
   * @param ForeignObject[] $objects
   */
   public function setForegnObjects(array $object)
   //..
}

Then in the code retrieving the objects:

$objects = $orm->findBy(MyObject::class, [...]);
$orm->loadManyToMany($objects, ForeignObject::class, 'link_table'); 

Chaining Loads

But what if you need to load objects on the objects you just loaded? These methods return the loaded objects so you can chain load calls like the following example:

$objects = $orm->findBy(MyObject::class, [...]);
$loadedForegnObjects = $orm->loadOne($objects, ForeignObject::class);
$loadedExampleObjects = $orm->loadMany($loadedForegnObjects, ExampleObject::class);
$orm->loadOne($loadedExampleObjects, AnotherObject::class);