PHP code example of timefrontiers / php-database-object
1. Go to this page and download the library: Download timefrontiers/php-database-object library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
timefrontiers / php-database-object example snippets
use TimeFrontiers\Helper\DatabaseObject;
final class UserRecord
{
use DatabaseObject;
protected static string $_db_name = 'application';
protected static string $_table_name = 'users';
protected static string $_primary_key = 'id';
protected static array $_db_fields = []; // Discovered when first needed.
public ?int $id = null;
public string $code = '';
public string $name = '';
}
UserRecord::useConnection($database);
$record = new UserRecord();
$record->setConnection($database);
$record->code = 'USER-1042';
$record->name = 'Example';
if (!$record->save()) {
$errors = $record->getErrors();
}
if (!$record->save()) {
$message = $record->firstError('_create');
$all = $record->getErrors();
}
$records = UserRecord::findBySql(
'SELECT COUNT(*) AS total, status FROM :db:.:table: ' .
'WHERE created_at >= ? GROUP BY status',
['2026-01-01']
);
$database->transaction(function (SQLDatabase $database): void {
InvoiceRecord::useConnection($database);
$record = new InvoiceRecord();
$record->setConnection($database);
// Assign ordinary infrastructure fields.
if (!$record->save()) {
// Translate model/connection errors at the repository boundary.
throw new PersistenceException('Invoice record could not be saved.');
}
});
use TimeFrontiers\Database\Schema\TableSchema;
TableSchema::clearCache('application', 'users');
TableSchema::clearCache('application');
TableSchema::clearCache();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.