| PHP |
|---|
|
public function CreateDataCollection(object $dataCollection , object $dataObject, array $rows) |
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.| 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.
}
}
|