velsym/database

Database management for PHP.

composer require velsym/database

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 takes properties 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 given URL.

    Returns: DatabaseConfig

    Params:

    • URL - string of database connection url.

  • setDriver(driver) - takes a DatabaseDriver enum as a parameter.

    Returns: self

    Params:

  • 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:

  • 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 given query and returns PDOStatement or false on failure.

    Returns: false|PDOStatement

    Params:

    • query - string of SQL query.

  • varTypeToDbType(type) - given the string of the PHP type the method will return type corresponding to the database type.

    Returns: string

    Params:

    • type - string name of the type.

  • getModelColumns(model) - given the model, 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:

  • isUsingBaseModel(model) - checks if given class/model is extended by BaseModel.

    Returns: bool

    Params:

    • model - class to be checked if is extended by BaseModel.

  • saveModel(modelInstance) - given the model object DatabaseManager will try to save the model or update it, if it already exists in the database.

    Returns: BaseModel extended class instance

    Params:

    • modelInstance - instance of class that is extended by BaseModel.

  • getModel(modelClass, params[]) - DatabaseManager will try to find model from modelClass table based on WHERE params.

    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 the model class, DatabaseBuilder will build query and return it.

    Returns: string|NULL

    Params:

  • buildModelTable(model) - given the model class, DatabaseBuilder will build a table in the database for the given model.

    Returns: void

    Params:


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 SQL WHERE property = value.

    Returns: self

    Params:

    • property - string name of the model property.

    • value - string or number.

  • greater(property, value) - equivalent of SQL WHERE property > value.

    Returns: self

    Params:

    • property - string name of the model property.

    • value - string or number.

  • lesser(property, value) - equivalent of SQL WHERE property < value.

    Returns: self

    Params:

    • property - string name of the model property.

    • value - string or number.

  • greaterOrEqual(property, value) - equivalent of SQL WHERE property >= value.

    Returns: self

    Params:

    • property - string name of the model property.

    • value - string or number.

  • lesserOrEqual(property, value) - equivalent of SQL WHERE property <= value.

    Returns: self

    Params:

    • property - string name of the model property.

    • value - string or number.

  • between(property, min, max) - equivalent of SQL WHERE 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 SQL WHERE property LIKE value.

    Returns: self

    Params:

    • property - string name of the model property.

    • value - string or number.

  • limit(limit) - equivalent of SQL LIMIT 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