Sample File: insert-callback.php
The Code
<?php
//outline (see source for full code)
require_once("include/include.php");
//create a new Customer row
$Customer = $GLOBALS["db"]["Customer"]->GetNewRow();
//note- as a shortcut to GetNewRow(), we could simply do:
//$Customer = $GLOBALS["db"]["Customer"]();
// -- NEW ROW --
//set the customer row cells with data
$Customer["Name"] = "Miss Piggy";
$Customer["EmailAddress"] = "queen@muppets.com";
$Customer["Gender"] = "Female";
$Customer["EmailVerified"] = 1;
//attach customer row events to auto-update our date cells
//it really doesn`t make sense to use callbacks like this. see advanced examples
$Customer->OnBeforeInsert( function($eventObject, $eventArgs){
$eventObject["DateCreated"] = gmdate("Y-m-d H:i:s T");
});
$Customer->OnBeforeCommit( function($eventObject, $eventArgs){
$eventObject["DateLastModified"] = gmdate("Y-m-d H:i:s T");
});
// -- AFTER SET FIELDS, BEFORE COMMIT --
//check for errors before we commit
//HasErrors() returns nothing when there are no errors
if( ($errors=$Customer->HasErrors()) ){
echo "Errors found: ".$errors;
}
else{
//commit this row to the database
$numRowsUpdated = $Customer->Commit();
}
// -- AFTER COMMIT --
?>
https://github.com/cirkuitnet/PHP-SmartDB/blob/master/samples/basic/insert-callback.phpDebugging Output
New Row
CustomerId:Name:
EmailAddress:
Gender:
EmailVerified: 0
DateLastModified:
DateCreated:
IsDirty:
Exists:
HasErrors: 'Name' field is required.
'Email Address' field is required.
'Gender' field is required.
Row After Set Fields, Before Commit
CustomerId:Name: Miss Piggy
EmailAddress: queen@muppets.com
Gender: Female
EmailVerified: 1
DateLastModified:
DateCreated:
IsDirty: 1
Exists:
HasErrors:
Commit() - Number Of Rows Affected
1Row After Commit
CustomerId: 3Name: Miss Piggy
EmailAddress: queen@muppets.com
Gender: Female
EmailVerified: 1
DateLastModified: 2024-11-21 08:44:57
DateCreated: 2024-11-21 08:44:57
IsDirty:
Exists: 1
HasErrors: