Download the PHP package irfantoor/database without Composer
On this page you can find all versions of the php package irfantoor/database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package database
Irfan's Database
Create models and/or access your databases with ease and least overhead. A bare-minimum and simple database access.
Installation
Creating a Database object
method: new Database(?array $connection = null)
parameters:
- array $connection - Connection array containing the parameters required by the Database Engines like MySQL, SQLite ...
returns: Database object
example:
Connect to a Database Engine
method: connect(array $connection)
parameteres:
- array $connection
returns:
- true - If the the database engine was successfully connected
- false - If could not connect to the engine
example:
Actions passed to database engine
Executes a raw SQL
method: query(string $sql, array $data = [])
parameteres:
- string $sql,
- array $bind associative array to bind data in sql while preparing
returns:
- true or false - if the query is of the type UPDATE, INSERT and DELETE
- array - returns the result of the SELECT query
example:
Inserts a record into a connected database
method: insert(string $table, array $record, array $bind = [])
parameteres:
- string $table - The table to be queried
-
array $record - associative array of record
Note: record must contain all of the required fields
- array $bind - associative array e.g.
returns:
- true - if the record was inserted
- false - record was not inserted
example:
Updates an existing record
method: update(string $table, array $record, array $options = [])
parameteres:
- string $table
-
array $record associated array only includes data to be updated
-
array $options contains where, limit or bind etc.
If options are not provided following are the assumed defaults:
- 'where' => '1 = 1',
- 'limit' => 1, // see DatabaseEngineInterface::get
- 'bind' => [],
returns:
- true - if successful
- false - otherwise
example:
Removes a record from database
method: remove(string $table, array $options)
parameteres:
- string $table
- array $options contains where, limit or bind options If options are not provided following are the assumed defaults:
returns:
- true - if removed successfully
- false - otherwise
example:
Retreives list of records
method: get(string $table, array $options = [])
parameteres:
- string $table
- array $options - Associative array containing where, order_by, limit and bind
If limit is an int, the records are retrived from start, if its an array it is interpretted like [int $from, int $count], $from indicates number of records to skip and $count indicates number of records to retrieve.
returns:
array [row ...] containing the array of rows or null if not found
example:
Retreives only the first record
method: getFirst(string $table, array $options = []);
parameteres:
- string $table name of the table e.g. $table = 'useres';
- array $options as explained in DatabaseEngineInterface::get
returns:
array containing the associative key=>value pairs of the row or null otherwise
example:
Database Models
NOTE: Currently Models only supports SQLite db
Models use the database and calls as explained above. Since a model is tied to a table, therefore the same calls (of database) apply to a model except that the first prameter of table_name is not present in the methods.
Creating a model
example: Models\Users.php
Model constructor
method: $users = new Users(array $connection)
parameteres:
- array $connection - ['file' => $db_path . 'users.sqlite', 'table' => 'users']
returns:
Users model object
example:
Retrieves the name of the database file
method: getDatabaseFile()
parameteres: none
returns:
string - pathname of the sqlite file the model is connected to
example:
Prepares a schema of the datbase from model definition and returns it
method: prepareSchema()
parameteres: none
returns:
string - Raw SQL schema, prepared from the definition of schema and indices, which were provided while wrinting the model (ref: Creating a Model), is returned. This schema can be used to create the sqlite file manually.
example:
Deploy the schema
method: deploySchema(string $schema)
parameteres:
- string $schema - The schema to be deployed to the connected file
throws: Exception - in case of error
returns: nothing
example:
Insert a record
method: insert(array $record, array $bind = [])
parameteres:
-
array $record Asociative array of record,
values might contain variables of the form :id etc, which are filled using the prepare mechanism, taking data from bind array e.g. ['id' => :id, 'name' => :name ] Note: record must contain all of the required fields
- array $bind - The data we need to bind to the :placeholders in $record
returns:
- true - if inserted the record successfully
- false - otherwise
example:
Insert or update a record
This method inserts the record if the record deoes not exist, or updates the existing one.
method: insertOrUpdate(array $record, array $bind = [])
parameteres:
- array $record - Associative array represnting one record
- array $bind - The data we need to bind to the :placeholders in $record
returns:
- true - if inserted or updated the record successfully
- false - otherwise
example:
Update an existing record
method: update(array $record, array $options = [])
parameteres:
- array $record - Associative array represnting one record
- array $options - The where clause or the binding data etc.
returns:
- true - if updated the record successfully
- false - otherwise
example:
Remove an existing record
method: remove(array $options)
parameteres:
- array $options - The where clause or the binding data etc.
returns:
- true - if removed the record successfully
- false - otherwise
example:
Retrieve a list of records
method: get(array $options = [])
parameteres:
- array $options - The where clause or the binding data etc.
returns: array or records or null
example:
Retrieve the first record
method: getFirst(array $options = [])
parameteres:
- array $options - The where clause or the binding data etc. this might include the order_by and limit parameters
returns:
- array - an associative array containing the record
- null - if could not find one
example:
Verify if a record exists
method: has($options = [])
parameteres:
- array $options - The where clause or the binding data etc.
returns:
- true - if record exists
- false - otherwise
example: