/ LJCPHPCodeDoc / LJCDataManagerLib / LJCDataManager / CreateDataObject


Parameters
$dataObject - A Data Object to use as a template.
$row - An array of data columns.

Returns

A data object record.

Syntax

PHP
public function CreateDataObject($dataObject, array $row)

Populates a typed Data Object with values from a DB row.

Remarks

This method provides Object to Relational Mapping (ORM) as it maps the data columns to properties in the DataObject and copies the matching values.

The data column keys which are the result set column names must match property names in the DataObject.

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;
 }
 
 // Populates a Data Object with values from a Data Result row.
 private static function CreateDataObject(LJCDataManager $manager)
 {
   $methodName = "CreateDataObject";
 
   $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.
   $item = new Item();
   $item = $manager->CreateDataObject($item, $row);
   if (null == $item)
   {
     echo("\$item was not created.");
   }
 
   // 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.