dcmunhoz/data-access

Data Access is a library designed to manipulate databases

1.0.3 2019-11-24 19:55 UTC

This package is auto-updated.

Last update: 2024-06-25 07:11:26 UTC


README

Data Access

Data Access is a library designed to manipulate databases

Highlights

  • Easy installation;
  • RAW queries execution;

Installation

On your prompt use the following command:

composer require dcmunhoz/data-access

Connection

To beegin, you need to connect to the database (MySQL / Postgress), check for more databases connections Here.

Define the following configuration constant:

define("DATA_ACCESS", [
  "driver"   => "mysql",
  "host"     => "localhost",
  "port"     => "3306",
  "dbname"   => "DATA_ACCESS_EXAMPLE",
  "user"     => "root",
  "password" => "",
  "options"  => [
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
  ]
]);

Your model

In your controller you can extends data-access or create an object.

/**
 * User controller example
 */
class UserController extends DataAccess{
  
    /**
     * User Constructor
     */
    function __construct(){
        /** \DataAccess::_construct(ENTITY_NAME, PRIMARY_KEY) */
        parent::__construct("TB_USERS", "ID_USER");
    }
}

RAW execution

RAW executions allows you to execute whatever query you want.

$user = new UserController();

$result = $user->raw("INSERT INTO TB_USERS(USER_NAME, PASSW, ID_PROFILE) VALUES(:uname, :upass, :pid)", [
    ":uname" => "user25",
    ":upass" => "mysupersecretpassword",
    ":pid" => '1'
]);

On INSERT, UPDATE or DELETE statement, RAW execution will return true in case of success or false if query fails.

On SELECT statement it will return an array with all found records.

fetch

fetch() will execute the query, if pass false to params, it will return only the first match, if pass true, will return all records

$user = new UserController();

//$result = $user->...->fetch(false);
$result = $user->...->fetch(true);

print_r($result);

find

find() will return all records from class entity

$user = new UserController();

$result = $user->find()->fetch(true);

print_r($result);

filter

filter() allows you to create a where statement to find specific records

$user = new UserController();

$result = $user->find()->filter("USER_NAME = :uname AND ID_USER = :uid", [":uname" => "USER1", ":uid" => 1])->fetch();

print_r($result);