PHPackages                             nicollassilva/simplephp - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. nicollassilva/simplephp

AbandonedArchivedLibrary[Database &amp; ORM](/categories/database)

nicollassilva/simplephp
=======================

A small CRUD project designed to assist daily routines and speed up the process of communicating with the database.

v1.9.0(5y ago)14452MITPHPPHP ^7.2.5

Since Jul 20Pushed 4y ago1 watchersCompare

[ Source](https://github.com/nicollassilva/SimplePHP)[ Packagist](https://packagist.org/packages/nicollassilva/simplephp)[ Docs](https://www.nicollas.site)[ RSS](/packages/nicollassilva-simplephp/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (1)Versions (17)Used By (0)

\[DESCONTINUED\] Simple PHP - Simple and easy CRUD for PHP projects
===================================================================

[](#descontinued-simple-php---simple-and-easy-crud-for-php-projects)

> OBS: This package was replaced by the [MinasORM](https://github.com/nicollassilva/minasorm) package.

[![Maintainer](https://camo.githubusercontent.com/70c19da2a3ddae13529df7fb1d4998cb4aecaa61c7122b98b77fca7e1318a57d/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6d61696e7461696e65722d406e69636f6c6c617373696c76612d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/nicollassilva)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7842129d19093c088ee4c2f3dcdb2631fd078255cedf41bf097cae231dc1985e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e69636f6c6c617373696c76612f53696d706c655048502f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nicollassilva/SimplePHP/?branch=master)[![PHP from Packagist](https://camo.githubusercontent.com/c9d879ea26bc48ed2c4fb71360a779c39dab280ae41ba90830199accef23953c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6e69636f6c6c617373696c76612f73696d706c657068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nicollassilva/simplephp)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Latest Version](https://camo.githubusercontent.com/1362bb9b9cf5661ae5005359c6a543d5a219dc63223e038fc8626cc1627aca74/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6e69636f6c6c617373696c76612f73696d706c657068702e7376673f7374796c653d666c61742d737175617265)](https://github.com/nicollassilva/simplephp/releases)[![Build Status](https://camo.githubusercontent.com/598e7e5a2bba26f4a1de070a9a5276f9cb60e1810b42149e9a0199e5cf4f328f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e69636f6c6c617373696c76612f53696d706c655048502f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nicollassilva/SimplePHP/build-status/master)[![Code Intelligence Status](https://camo.githubusercontent.com/a3ef5c8366ffd5d759ab6662624b743646c6bf08722605d044531de998840787/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e69636f6c6c617373696c76612f53696d706c655048502f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)[![Total Downloads](https://camo.githubusercontent.com/42c2123956510d803486d2e366c29a30f789e568dc0b9304a2ce8537b1600a37/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e69636f6c6c617373696c76612f73696d706c657068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/nicollassilva/simplephp)

###### Um pequeno projeto CRUD desenvolvido para auxiliar as rotinas diárias e acelerar o processo de comunicação com o banco de dados com segurança e transparência.

[](#um-pequeno-projeto-crud-desenvolvido-para-auxiliar-as-rotinas-diárias-e-acelerar-o-processo-de-comunicação-com-o-banco-de-dados-com-segurança-e-transparência)

A small CRUD project developed to assist daily routines and speed up the process of communicating with the database with security and transparency.

Getting Started
---------------

[](#getting-started)

Some precautions and step-by-step instructions for you to download and install the package.

### Prerequisites

[](#prerequisites)

To be able to use **SimplePHP** you will need to have:

```
PHP ^7.2.5
EXT-PDO *

```

### Installing

[](#installing)

SimplePHP can be installed via **composer.json** or via the **command terminal**:

```
composer require nicollassilva/simplephp

```

or **composer.json**:

```
"nicollassilva/simplephp": "^1.9"
>>>>>>> Stashed changes

```

#### Connection

[](#connection)

To configure the connection to the database, you must access: **Source\\Root\\Config.php**. Example of the file to be found:

```
protected $config = [
        "driver" => "mysql",
        "hostname" => "localhost",
        "charset" => "utf8mb4",
        "port" => 3306,
        "username" => "root",
        "password" => "",
        "database" => "",
        "timezone" => "America/Sao_Paulo",
        "pathLog" => __DIR__ . "/../../../../../your-log.log",
        "options" => [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_CASE => PDO::CASE_NATURAL,
            PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING
        ]
    ];
```

#### Example of your model

[](#example-of-your-model)

After completing the database configuration, create a folder at the root of the project where your **Models** will be and create the class.

- You should extend and use the SimplePHP class namespace, as in the example:

```
namespace Models;

use SimplePHP\Model\SimplePHP;

class User extends SimplePHP {

    function __construct()
    {
        /**
         * @param string Table Name
         * @param string Primary Key
         */
        parent::__construct('users', 'id');
    }
}
```

- You will need to inherit your model from the SimplePHP class, and in the magic constructor method, call the parent constructor with the name of the table for the referred model, and a primary key.

#### First use

[](#first-use)

After all the previous steps, create an index.php at the root of the project giving a **require in the composer autoload and in your model class**, after that, instantiate your model and you can start using SimplePHP. Below is an example:

```
    require "vendor/autoload.php";
    require "models/user.php";

use Models\User;

    $userModel = new User();
    $user = $userModel->find()->execute();
```

### Installation Errors

[](#installation-errors)

#### Some mistakes and how to fix them

[](#some-mistakes-and-how-to-fix-them)

> Fatal error: Uncaught Error: Class 'SimplePHP\\Model\\SimplePHP' not found

To fix it, execute the following command in the project's root folder:

```
composer dump -o

```

Documentation
-------------

[](#documentation)

### Methods

[](#methods)

#### find

[](#find)

```
use Models\User;

$userModel = new User();

/** find all users */
$user = $userModel->find()->execute();

/** find user by id */
$user = $userModel->find(5)->execute();

/** find users and return the total result count */
$count = $userModel->count()->execute();

/** find user with one where */
$user = $userModel->where([
                                     ['name', '=', 'Nicollas']
                                ])->execute();

/** find user with several where. Conditional AND default */
$user = $userModel->where([
                                     ['name', '=', 'John'],
                                     ['email', '=', 'johnmoppans@gmail.com']
                                ])->execute();

/** find user with LIKE. Conditional AND default */
$user = $userModel->where([
                                     ['name', 'LIKE', '%Guilherme%'],
                                     ['email', '=', 'guilherme@gmail.com']
                                ])->execute();

/** find user with conditional where. Condicional OR */
$user = $userModel->where([
                                     ['name', 'LIKE', '%Nicollas%'],
                                     ['name', 'LIKE', '%Nicolas%']
                                ], 'OR')->execute();

/** find users by name = Nicollas OR name = Nicolas */
$user = $userModel->where([
				     ['name', '=', 'Nicollas']
			])
			->orWhere([
				     ['name', '=', 'Nicolas']
			])->execute();

/** find user using the basic structure query */
$user = $userModel->whereRaw("name = 'Nicollas'")->execute();

/** find users with limit */
$user = $userModel->limit(5)->execute();

/** find users with limit & offset */
$user = $userModel->limit(5)->offset(5)->execute();

/** find users with skip & take | the same as limit & offset */
$user = $userModel->take(5)->skip(5)->execute();

/** find users with orderBy. second parameter optional, default ASC */
$user = $userModel->orderBy('id', 'DESC')->orderBy('name')->execute();

/** find users and return results as attributes. EXAMPLE: $user->name instead of $user['name'] */
$user = $userModel->find()->execute(true);

/** find users with specific columns. */
$user = $userModel->find()->only(['name', 'id', 'email'])->execute();

/** find users creating exceptions in columns. */
$user = $userModel->find(5)->except(['password'])->execute();

/** search in other database table */
$user = (new User())->useTable('posts')->find()->where([['owner_id', '=', $user->id]])->execute();

/** debug query for possible errors | return string */
$user = $userModel->whereRaw("uuid = 'f031b537-672f-4fba-b8a6-af45e778ad93'")->debugQuery();

/** group by method */
$user = $userModel->groupBy('id');
```

- **Note:** *except()* method does not work chained with the *execute(true)* method, only *execute()* without parameter true.
- **Note:** *except()* method only works when looking for specific information, in multidimensional arrays it does not work. This will be fixed soon.
- All methods are friendly to each other, that means you can make complex queries. Real example:

```
/** Search for a user's posts varying between the privacy in which it was saved */
/** You can pass false as a fourth argument, the class will understand that you are trying to relate tables and will not put single quotes */

$posts = (new User())->useTable('posts p, users u')
                             ->where([['u.id', '=', $user['id'] ?? $visit['id']],
                                      ['u.id', '=', 'p.user_id', false], /** example of the fourth argument for relating tables */
                                      ['p.privacity', '
