velsym/database
Database management for PHP.
composer require velsym/databaseVelsym\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:
intsetId()- 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::MYSQLDatabaseDriver::POSTGRESQLDatabaseDriver::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 takespropertieslisted above, however can be also instantiated without any parameters. Can be configured after instantiation.Returns:
DatabaseConfigParams:
properties- (optional) same properties as those listed above.
fromURL(URL)- static method that returns fully configured DatabaseConfig based on the givenURL.Returns:
DatabaseConfigParams:
URL- string of database connection url.
setDriver(driver)- takes a DatabaseDriver enum as a parameter.Returns:
selfParams:
driver- DatabaseDriver.
setHostname(hostname)- sets hostname.Returns:
selfParams:
hostname- string of hostname.
setUsername(username)- sets username.Returns:
selfParams:
username- string of username.
setPassword(password)- sets user's password.Returns:
selfParams:
password- string of password.
setDatabase(database)- sets database name.Returns:
selfParams:
database- string of database name.
setPort(port)- sets database's port.Returns:
selfParams:
port- int of database's port.
setOptions(options[])- sets database's connection options.Returns:
selfParams:
options[]- array of database's connection options.
setSocket(socket)- sets socket.Returns:
selfParams:
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:
voidParams:
config- DatabaseConfig to connect to the database.
getConfig()- returns config or null if config hasn't been set yet.Returns:
DatabaseConfig|NULLlastInsertId()- returns the id of the last inserted row or sequence value.Returns:
false|stringquery(query)- executes givenqueryand returns PDOStatement or false on failure.Returns:
false|PDOStatementParams:
query- string of SQL query.
varTypeToDbType(type)- given the string of the PHPtypethe method will return type corresponding to the database type.Returns:
stringParams:
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|NULLParams:
model- class extending BaseModel.
isUsingBaseModel(model)- checks if givenclass/modelis extended by BaseModel.Returns:
boolParams:
model- class to be checked if is extended by BaseModel.
getModel(modelClass, params[])- DatabaseManager will try to find model frommodelClasstable based on WHEREparams.Returns:
BaseModelextended class instance or NULLParams:
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:
boolParams:
model- instance of class extending BaseModel.
DatabaseBuilder
Static class for building structure of your SQL Database.
Methods
buildModelTableQuery(model)- given themodelclass, DatabaseBuilder will build query and return it.Returns:
string|NULLParams:
model- class extending BaseModel.
buildModelTable(model)- given themodelclass, DatabaseBuilder will build a table in the database for the given model.Returns:
voidParams:
model- class extending BaseModel.
ModelManager
Model management simplifed.
Methods
__construct(NULL|model)- sets model which is being managed.Returns:
ModelManagerParams:
model- model class to hint ModelManager from which database table look for.
useModel(model)- set/change model.Returns:
voidParams:
model- model class to hint ModelManager from which database table look for.
fetch()- first command when building query.Returns:
selfwhere()- entrypoint for building the where section of the query.Returns:
selfand()- keyword to chain wheres (equal, greater, lesser, ...).Returns:
selfor()- keyword to chain wheres (equal, greater, lesser, ...).Returns:
selfequal(property, value)- equivalent of SQLWHERE property = value.Returns:
selfParams:
property- string name of the model property.value- string or number.
greater(property, value)- equivalent of SQLWHERE property > value.Returns:
selfParams:
property- string name of the model property.value- string or number.
lesser(property, value)- equivalent of SQLWHERE property < value.Returns:
selfParams:
property- string name of the model property.value- string or number.
greaterOrEqual(property, value)- equivalent of SQLWHERE property >= value.Returns:
selfParams:
property- string name of the model property.value- string or number.
lesserOrEqual(property, value)- equivalent of SQLWHERE property <= value.Returns:
selfParams:
property- string name of the model property.value- string or number.
between(property, min, max)- equivalent of SQLWHERE property BETWEEN min AND max.Returns:
selfParams:
property- string name of the model property.min- minimum value.max- maximum value.
like(property, value)- equivalent of SQLWHERE property LIKE value.Returns:
selfParams:
property- string name of the model property.value- string or number.
limit(limit)- equivalent of SQLLIMIT limit.Returns:
selfParams:
limit- limit amount of queries to n results.
getSQL()- returns fully built SQL query.Returns:
stringquery()- 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:
Modelsame as the one passed, but is retrieved from the database.Params:
model- model which is meant to be saved/updated.
Last updated