Skip to content

Latest commit

 

History

History
176 lines (111 loc) · 3.13 KB

doc-en.md

File metadata and controls

176 lines (111 loc) · 3.13 KB

Installation

The easiest way to install is via composer:

composer require ernandesrs/easy-crud

Configuration

The configuration is quite simple, you just need to create a database and define the constant below in your project settings. It has the necessary information and configurations to access your database.

define("CONF_EASY_CRUD", [
    "dbname" => "database name",
    "host" => "localhost",
    "user" => "database username",
    "pass" => "database password",
    "options" => [
        \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_CLASS,
        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
    ]
]);

Usage

Usage is also quite simple, just create an instance of EasyCrud, passing the name of the table and the name of the table's primary key column to the constructor.

See below an example for use in a users table, whose primary key column is iduser:

<?php

require __DIR__ . "/../vendor/autoload.php";

$user = new \ErnandesRS\EasyCrud\EasyCrud("users", "iduser");

The second parameter is optional if the column primary key is id.

Query examples

Below are some examples of how to query a user table.

Retrieving a single record

$user = $user->getOne();
var_dump($user);

Retrieving a single record with some specific columns

$user = $user->getOne("id, first_name, username");
var_dump($user);

Retrieving a record by primary key

$user = $user->find(10);
var_dump($user);

Retrieving a record by primary key with some specific columns

$user = $user->find(10, "id, first_name, username");
var_dump($user);

Getting all records

$all = $user->getAll();
var_dump($all);

Getting all records with some specific columns

$all = $user->getAll("id, first_name, username");
var_dump($all);

Get records that meet some conditions with the AND operator

$all = $user->where("id", "<=" , 5)->getAll();
var_dump($all);

Get records with conditions using the OR operator

$all = $user->where("id", "<=", 3)->orWhere("id", "=" , 10)->getAll();
var_dump($all);

Limiting results

$all = $user->limit(10)->getAll();
var_dump($all);

Other methods

public function whereNull(string $field);
public function whereNotNull(string $field);
public function orWhereNull(string $field);
public function orWhereNotNull(string $field);
public function offset(int $offset);

Insert example

See below an example of how to perform record insertions in a table.

$new = $user->create([
    "first_name" => "John"
    "last_name" => "Marinheiro"
]);
var_dump($new);

Update example

Below is an example of how to update a record.

$user = (new EasyCrud("users"))->find(54);
if($user) {
    $user->update([
        "first_name" => "New Name",
        "last_name" => "New Last Name",
        "username" => "newusername",
        "gender" => "m"
    ]);
 }

Exclusion example

See below for an example of how to delete a record.

$user = (new EasyCrud("users"))->find(54);
if($user) {
    $user->delete();
}