losthost / db
Database access with so called DB Objects (descendants of class DBObject)
Installs: 153
Dependents: 10
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:class
Requires
- php: ^8.1.1
Requires (Dev)
- phpunit/phpunit: 10.4.2
- dev-main
- 3.0.13-beta
- v3.0.12-beta
- v3.0.11-beta
- v3.0.10-beta
- v3.0.9-alpha
- v3.0.8-alpha
- v3.0.7-alpha
- v3.0.6-alpha
- v3.0.5-alpha
- v3.0.4-alpha
- v3.0.2-alpha
- v3.0.1-alpha
- v3.0.0-alpha
- v2.0.1
- v2.0.0
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.8
- v1.4.7
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- 1.4.1
- 1.4.0
- v1.3.0
- v1.2.3
- v1.2.2
- 1.2.1
- v1.1.1
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-master
- dev-Kuzma
This package is auto-updated.
Last update: 2024-12-16 04:34:35 UTC
README
Database access with so called DB Objects (descendants of class DBObject) that mirror data table structure to your project
DB - making DB connection
There is an example class DBChildObjectExample that is a descendant of DBObject. Before using DBObject descendant you have to open DB connection with and make some stuff:
DB::connect($db_host, $db_user, $db_pass, $db_name, $db_prefix='', $db_encoding='utf8mb4');
DB::setClassNamespace("\\your\\namespace\\"); /* use \\ at the begining and the end of namespace */
DB::checkDataStructure("yourClassOne, yourClassTwo, andSoOn", true);
After that you can access a table with your DBObject descendant.
DBObject - accessing data
Some useful operations:
// Creating record:
$new = new DBChildObjectExample();
$new->name = "test";
$new->description = "A test record";
$new->write();
$new_record_id = $new->id;
// Accessing record:
$existing = new DBChildObjectExample("id = ?", $new_record_id);
if ($existing->isNew()) {
throw new \Exception('Object not found');
} else {
// Modify object:
$existing->description = 'New description';
$existing->write();
}
Special stuff
If you need some special processing (like checking or filling data) you may override DBObject::fetch(...) and DBObject::write() methods.
TODO
- добавить параметр формата к DBObject->asString();
Epilogue
That's it. Have fun.