velsym/database
Database management for PHP.
Velsym\Database
BaseModel
Foundation for all models. All models represent a database record.
Properties
TABLE_NAME
- constant representing name of the database table.id
- model/database record id. If model has been created in the application and not retrieved from the database then it will be equal to -1.
Methods
getId()
- returns the id of the model. If model wasn't retrieved from database (meaning it was instantiated in PHP and not saved to the database) then it will return -1 (hinting that it's not present in the database).Returns:
int
setId()
- id shouldn't be set manually, so setId has been "locked" for that reason. Doesn't do anything.Returns:
void
DatabaseDriver
Enum telling DatabaseManager how to build queries. Doesn't actually work yet. Work in progress.
Enums
DatabaseDriver::MYSQL
DatabaseDriver::POSTGRESQL
DatabaseDriver::SQLITE
DatabaseConfig
Readonly class containing data required to connect to the database via DatabaseManager.
Properties
driver
- takes a DatabaseDriver enum as a parameter.hostname
- database hostname.username
- database username.password
- database user's password.database
- database name.port
- database port.options[]
- database's connection options. A key-value array where key is option name and value is option's value.socket
- database socket
Methods
__construct(properties)
- DatabaseConfig takesproperties
listed above, however can be also instantiated without any parameters. Can be configured after instantiation.Returns:
DatabaseConfig
Params:
properties
- (optional) same properties as those listed above.
fromURL(URL)
- static method that returns fully configured DatabaseConfig based on the givenURL
.Returns:
DatabaseConfig
Params:
URL
- string of database connection url.
setDriver(driver)
- takes a DatabaseDriver enum as a parameter.Returns:
self
Params:
driver
- DatabaseDriver.
setHostname(hostname)
- sets hostname.Returns:
self
Params:
hostname
- string of hostname.
setUsername(username)
- sets username.Returns:
self
Params:
username
- string of username.
setPassword(password)
- sets user's password.Returns:
self
Params:
password
- string of password.
setDatabase(database)
- sets database name.Returns:
self
Params:
database
- string of database name.
setPort(port)
- sets database's port.Returns:
self
Params:
port
- int of database's port.
setOptions(options[])
- sets database's connection options.Returns:
self
Params:
options[]
- array of database's connection options.
setSocket(socket)
- sets socket.Returns:
self
Params:
socket
- string of socket.
getDSN()
- returns DSN.Returns:
string
DatabaseManager
Static class that manages your database.
DatabaseManager is under heavy development. Some of the methods listed below may not exist soon!
Methods
setConfig(config)
- sets config to database connection.Returns:
void
Params:
config
- DatabaseConfig to connect to the database.
getConfig()
- returns config or null if config hasn't been set yet.Returns:
DatabaseConfig
|NULL
lastInsertId()
- returns the id of the last inserted row or sequence value.Returns:
false|string
query(query)
- executes givenquery
and returns PDOStatement or false on failure.Returns:
false|
PDOStatement
Params:
query
- string of SQL query.
varTypeToDbType(type)
- given the string of the PHPtype
the method will return type corresponding to the database type.Returns:
string
Params:
type
- string name of the type.
getModelColumns(model)
- given themodel
, method will returns a key-value array where key is name of the column and value is the type of the column.Returns:
array|NULL
Params:
model
- class extending BaseModel.
isUsingBaseModel(model)
- checks if givenclass/model
is extended by BaseModel.Returns:
bool
Params:
model
- class to be checked if is extended by BaseModel.
getModel(modelClass, params[])
- DatabaseManager will try to find model frommodelClass
table based on WHEREparams.
Returns:
BaseModel
extended class instance or NULL
Params:
modelClass
- model class from which record should be retrieved.params[]
- key-value array where key is column name and value is column value.
isPresentInDatabase(model)
- given the model object DatabaseManager will check if the model already exists in the database.Returns:
bool
Params:
model
- instance of class extending BaseModel.
DatabaseBuilder
Static class for building structure of your SQL Database.
Methods
buildModelTableQuery(model)
- given themodel
class, DatabaseBuilder will build query and return it.Returns:
string|NULL
Params:
model
- class extending BaseModel.
buildModelTable(model)
- given themodel
class, DatabaseBuilder will build a table in the database for the given model.Returns:
void
Params:
model
- class extending BaseModel.
ModelManager
Model management simplifed.
Methods
__construct(NULL|model)
- sets model which is being managed.Returns:
ModelManager
Params:
model
- model class to hint ModelManager from which database table look for.
useModel(model)
- set/change model.Returns:
void
Params:
model
- model class to hint ModelManager from which database table look for.
fetch()
- first command when building query.Returns:
self
where()
- entrypoint for building the where section of the query.Returns:
self
and()
- keyword to chain wheres (equal, greater, lesser, ...).Returns:
self
or()
- keyword to chain wheres (equal, greater, lesser, ...).Returns:
self
equal(property, value)
- equivalent of SQLWHERE property = value
.Returns:
self
Params:
property
- string name of the model property.value
- string or number.
greater(property, value)
- equivalent of SQLWHERE property > value
.Returns:
self
Params:
property
- string name of the model property.value
- string or number.
lesser(property, value)
- equivalent of SQLWHERE property < value
.Returns:
self
Params:
property
- string name of the model property.value
- string or number.
greaterOrEqual(property, value)
- equivalent of SQLWHERE property >= value
.Returns:
self
Params:
property
- string name of the model property.value
- string or number.
lesserOrEqual(property, value)
- equivalent of SQLWHERE property <= value
.Returns:
self
Params:
property
- string name of the model property.value
- string or number.
between(property, min, max)
- equivalent of SQLWHERE property BETWEEN min AND max
.Returns:
self
Params:
property
- string name of the model property.min
- minimum value.max
- maximum value.
like(property, value)
- equivalent of SQLWHERE property LIKE value
.Returns:
self
Params:
property
- string name of the model property.value
- string or number.
limit(limit)
- equivalent of SQLLIMIT limit
.Returns:
self
Params:
limit
- limit amount of queries to n results.
getSQL()
- returns fully built SQL query.Returns:
string
query()
- runs query and returns an array or single model depending on whether limit is 1.Returns:
Model|Model[]
save(model)
- saves/updates model to the database.Returns:
Model
same as the one passed, but is retrieved from the database.Params:
model
- model which is meant to be saved/updated.
Last updated