kingsoft / persist-db
Implementation of Kingsoft/IPersist for databases using Kingsoft/Database as a backend.
Requires
- php: >=8.2
- kingsoft/db: >=2.7
README
This implementation of the abstract Persist/Base
uses a Db
connection to make tables in the database available as persistent PHP objects. This generates a class for every table or view in the database extracting PK, possibly with auto increment from tables. With Mariadb it is not possible to extract PK from a view so manual setting the getPrimaryKey() is needed.
Setup
To create the PHP proxies to the tables and views use
https://example.com/vendor/kingsoft/persist-db/discover.php
which only works if the proper global SETTINGS array is available. (See Kingsoft\Utils
This will create a folder discovered
in the root with subfolders based on namespace in the settings file. It also responds with a page listing what is available. To have the proxies autoloaded add the psr-4 section to your composer.json
and run
composer dump-autoload
The allowedEndPoints
array can be used for a persist-rest
settings section.
The proxy objects work as a facade for database tables and can now used to
- Created a recordd in the database with freeze()
- Read from the databae with thaw(id)
- Updated by reading and changing the properties and freeze()
- Deleted by
thaw
ing anddelete
ing the php object
We have a CRUD object in this way. Yaj!
Searching
Searching is also possible be setting a where and order with a static findall
which gets a Generator
interface to use with foreach. A findFirst
, fineNext
, can be used in an Iterator
context using the Persist\IteratorTrait
. The object itself is a generater and can be used in a yield
loop.
Services
Persist\Base::createFromArray()
creates a record from an array representationPersist\Base::getJson()
retrieves a json representation of the object