Download the PHP package zobaken/daltron without Composer
On this page you can find all versions of the php package zobaken/daltron. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package daltron
Introduction
Database Abstraction Layer for PHP.
Created to be simply as possible tool for creating and running SQL queries. Currently supports mysql and postgresql drivers.
Requirements
PHP7, mysqli extension for Mysql support. pgsql extension for postgres..
Installation
Install it via composer:
Configuration
First we need to set configuration:
It is possible to load configuration from file. For example 'config.php' can look like:
Then we load it like this:
Query builder
Simplest query will look like:
Lets try something more complex. Builder mimics SQL syntax, so nothing new to learn:
Every "unknown" method of query, like select
method in this example adds term to
the SQL request. All parameters mapped with ?
placeholders, that are not integer,
are escaped and surrounded by quotes.
The previous example is equivalent to:
Here is a typical situation when query conditions depends on user input:
Additional conditions are added only when the first parameter of ifQuery
is
true. #?
placeholder is used for field name escaping. In this example we assume
that $orderDirection
equals ASC
or DESC
and !?
placeholder does not escape the value,
use it with caution!
Insert and update example (we assume that id field is autoincrement):
Method insertRow
is a shortcut for insert request.
dbtime
function without parameters returns current time in format Y-m-d H:i:s
.
Passing true
to exec
method we ask it to return last inserted id.
Method affectedRows
is used to get number of rows affected by previous query, obviously.
Models
Generation
To generate models we need already initialized database with existing data structure. In this example we use provided script utilizing configuration file from examples above:
Model classes will be created in "model" folder. If you need to create your own model generator scripts (with blackjack and closures) you can write something like this:
You can modify your model classes and if data structure is changed - run generator again. Model classes will not be overwritten, only their prototype classes.
Basic usage
Its your responsibility to load model classes using spl_autoload_register
function or whatever method
you like.
Here is a an example of using model classes:
Passing true
to insert
we ask it to return last inserted id.
Next we will get object from database and update it:
Simple as it!
Use remove
to delete the object:
Advanced model requests
We can get object by passing where condition to findRow
method:
Same for list of object using find
method:
This will return objects created earlier then day ago.
You can pass not only where condition, but some later part of request. It is possible to limit our request to return only 10 rows:
Also we can use regular query builder syntax. Here is slightly modified
request from example we had in somewhere above. Query will return an
array of User
objects.
Here is example of update request:
Same for delete:
Its easier to use queryUpdateRow
method to update several fields:
In this case "SET" statement will be generated for passed fields with values escaped.
Namespaces
By default model classes are created in root namespace. You can change it by
adding "namespace"
option to your config:
Configuration profiles
You can create more then one profile to access different databases. In examples above we used only one profile with name "default". To create more profiles you need to define them in configuration like this:
Now we have exactly same "default" profile as we had before and another one named "postgres".
Now we can use it by passing to our db
function:
Also we can pass profile to model generator, so the models will use it instead of "default":
License
Copyright (c) 2009-2018 Nikolay Neizvesny
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.