Download the PHP package flsouto/fstore without Composer
On this page you can find all versions of the php package flsouto/fstore. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package fstore
Accessing a Database
A database is nothing more than a directory. So all you have to do is specify the path to that directory when creating a database instance. If the directory does not exist, it will be created when the first row is inserted.
Accessing a Table
You can get a table instance from a database by calling the table
method. If the table does not exist, it will be
created once the first row is inserted.
Inserting and Retrieving a Row
The table object provides an insert
method which accepts an associative array and returns the automatically generated
row id:
Outputs:
Getting the Insert Date Based on the Row's ID
The insert
method of a table object generates an id which contains the timestamp itself so there is no need to create a "date_added" column.
But, in order to avoid conflicts when inserting multiple rows on the same table at the same time the generated id has the miliseconds as well as a counter
which guarantees that the ids will always be different, even if inserted on the same nanosecond! So, in order to take the corrrect date/time from this hash you have to remove those extra numbers.
The good news is that there is a $table->date
method which does just that for you:
Outputs:
Updating a Record
Updating a table record is pretty straightforward: just call update
and pass it an associative array followed by
the row id:
Outputs:
Deleting a Record
To delete a record simply call $table->delete($id)
, see example:
Retrieving all IDs from a Table
Use $table->ids()
to get all ids:
Outputs:
Retrieving only first 10 IDs from a Table
Use $table->ids(X)
to get the first X ids:
Outputs:
Retrieving only the LAST 10 IDs from a Table
Use $table->ids(-X)
to get the last X ids:
Outputs:
Querying a Table using Filters
The table object provides a query builder which can be created via $table->query()
.
This query builder allows us to filter results by means of a $query->filter()
function
which accepts a callback. The following example illustrates this better:
Outputs:
Querying The Last X Rows...
Call the limit
method on a query object and pass it a negative number. See example:
Outputs:
Fetching only one column from the result
Call the values
method on a query object passing it the name of the desired column.
This will return an array of all values of that column. See example:
Outputs:
Fetching only the ids from a result
Call the ids
method on a query object:
Outputs:
Selecting the generated ID as a column
Use the $query->selid($as_key) to include the row id in the result:
Outputs:
Filtering by creation date
Use the $query->since($date)
and/or $query->until($date)
to filter by creation date.
This is faster than filtering by a custom "date" column because it doesn't require the data to be loaded from disk
(the comparison is based on the generated ids themselves).
It's also possible to combine both:
The $query->since()
and $query->until()
methods accept any argument the strtotime()
php function would accept
as well as a DateTime
object or a date in the d/m/Y
format.