/ 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 $collection
, 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
 $webCommonPath = "c:/inetpub/wwwroot/LJCPHPCommon";
 require_once "$webCommonPath/LJCDataManagerLib.php";
 
 // See constructor for how to create $connectionValues.
 $manager = new LJCDataManager($connectionValues, "TableName");
 
 class Person
 {
   public int $ID;
   public string $Name;
 }
 
 $rows = $manager->Load(null);
 $persons = $manager->CreateDataCollection(new Persons(), new Person()
   , $rows);
 foreach ($persons as $person)
 {
   echo $person->ID;
   echo $person->Name;
 }

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