/ LJCPHPCodeDoc / LJCDataManagerLib / LJCDataManager / CreateDataCollection


Parameters
$collection - A Collection Object to use as a template.
$dataObject - A Data Object to use as a template.
$rows - An array of data records.

Returns

An array of Data object records or null if there are no rows.

Syntax

PHP
public function CreateDataCollection(object $dataCollection
, object $dataObject, array $rows)

Creates an array of typed Data Objects from a DB rows array.

Remarks

This method provides Object to Relational Mapping (ORM) as it maps an array of data records into a collection of strongly typed data objects. It calls the CreateDataObject() method for each data record and adds the resulting typed object to the strongly typed collection.

The collection must be an object that extends (inherits from) LJCCollectionLib.php class LJCCollectionBase and provides an AddObject method with the signature AddObject(DataObject $item, $key = null); Where DataObject is a strongly typed Data Object. See LJCDbAccessLib.php class LJCDbColumns and LJCDbColumn for examples.

The AddObject() method should supply a default key value or the method can be changed to require the programmer to enter it by removing the "= null" from the method signature.

The DataObject must supply a Clone() method.

Example

PHP
 class Item
 {
   public function __construct()
   {
     $this->ID = 0;
     $this->Name = "";
   }
 
   // Creates an object clone.
   public function Clone(): self
   {
     $retValue = new self();
 
     $retValue->ID = $this->ID;
     $retValue->Name = $this->Name;
     return $retValue;
   } // Clone()
 
   public int $ID;
   public string $PropertyName;
 }
 
 class Items extends LJCCollectionBase
 {
   // Adds an object and key value.
   public function AddObject(Item $item, $key = null): ?Item
   {
     $methodName = "AddObject()";
 
     if (null == $key)
     {
       $key = $item->Name;
     }
 
     // AddItem() is in LJCCollectionBase.
     $retItem = $this->AddItem($item, $key);
     return $retItem;
   } // AddObject()
 
   // Retrieves the item by index.
   public function RetrieveAtIndex($index)
   {
     $retItem = $this->RetrieveItemAtIndex($index);
     return $retItem;
   }
 }
 
 // Creates an array of Data Objects from a Data Result rows array.
 private static function CreateDataCollection(LJCDataManager $manager)
 {
   $methodName = "CreateDataCollection";
 
   $rows = $manager->Load();
   $row = $rows[0];
   $id = LJCDbAccess::GetValue($row, "ID");
   $name = LJCDbAccess::GetValue($row, "Name");
 
   // result:
   // $id = First row ID column value.
   // $name = First row Name column value.
 
   // See constructor for how to create $manager.
   $items = new Items();
   $item = new Item();
   $items = $manager->CreateDataCollection($items, $item, $rows);
   if (null == $items
     || 0 == count($items))
   {
     echo("\$items was not created.");
   }
   else
   {
     $item = $items->RetrieveAtIndex(0);
 
     // result:
     // $item->ID = First item ID column value.
     // $item->Name = First item Name column value.
   }
 }

Copyright © Lester J. Clark and Contributors.
Licensed under the MIT License.