PHPackages                             eftec/pdooneorm - 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. eftec/pdooneorm

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

eftec/pdooneorm
===============

Procedural PDO Data access class in a single Class

2.6(1y ago)25001LGPL-3.0-onlyPHPPHP ^7.4 || ^8.0

Since Mar 11Pushed 1y ago2 watchersCompare

[ Source](https://github.com/EFTEC/PdoOneORM)[ Packagist](https://packagist.org/packages/eftec/pdooneorm)[ Docs](https://github.com/EFTEC/PdoOneORM)[ RSS](/packages/eftec-pdooneorm/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (12)Used By (0)

Database Access Object wrapper for PHP and PDO in a single class
================================================================

[](#database-access-object-wrapper-for-php-and-pdo-in-a-single-class)

PdoOneORM. It's a simple ORM wrapper for PHP's PDO library compatible with SQL Server (2008 R2 or higher), MySQL (5.7 or higher) and Oracle (12.1 or higher).

This library tries to **work as fast as possible and simply as possible**. The complete library, including dependencies is less than 100 files (and less than 30 are code).

This library works differently to Eloquent. While Eloquent is code-first, this library is database first.

PdoOneORMEloquent and others code-first ORMDatabase first. You create the database and it will generate the codeCode First. You create the code and it will generate the databaseThis library is aware of the schema of the database, for example every column of every table.This library is not aware of the schema of the databaseRelations are created using foreign keys or manually.Relation could create foreign keys.Why?

[![Packagist](https://camo.githubusercontent.com/50f0a8f9b95ac605da1b38e9941226cd2fa6893002a3e8757c51dd15f009e8f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f65667465632f50646f4f6e654f524d2e737667)](https://packagist.org/packages/eftec/PdoOneORM)[![Total Downloads](https://camo.githubusercontent.com/0de12787ead286d8864ae179f39abcd2e394ef4b42f5f6329554e5eb027413ad/68747470733a2f2f706f7365722e707567782e6f72672f65667465632f50646f4f6e654f524d2f646f776e6c6f616473)](https://packagist.org/packages/eftec/PdoOneORM)![Maintenance](https://camo.githubusercontent.com/0c8f829897840ac35cb3daf181a719612c0f64c0ed5fca3c7b90ed7591169162/68747470733a2f2f696d672e736869656c64732e696f2f6d61696e74656e616e63652f7965732f323032352e737667)![composer](https://camo.githubusercontent.com/08623182d7b037246f11c0ad4aec3fe405dcf9d668058398497abd1a9a770e9d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6d706f7365722d253345312e362d626c75652e737667)![php](https://camo.githubusercontent.com/227bb49c4c0b54164298fa37d1b58ca6077ec80bd43db1c80725acc10729f2dd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e312d677265656e2e737667)![php](https://camo.githubusercontent.com/5cd91a78fb469ca20b235b6951fb6dd77bda78ac4633eb432e93699bcb141589/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e342d677265656e2e737667)![CocoaPods](https://camo.githubusercontent.com/347353606ed8f26b45bcf9da083db0063fa1dadd1baef36a5f3bf9ce1d127548/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63732d37302532352d79656c6c6f772e737667)

Turn this

```
$stmt = $pdo->prepare("SELECT * FROM myTable WHERE name = ?");
$stmt->bindParam(1,$_POST['name'],PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->get_result();
$products=[];
while($row = $result->fetch_assoc()) {
  $product[]=$row;
}
$stmt->close();
```

into this using the ORM.

```
ProductRepo // this class was generated with echo $pdoOne()->generateCodeClass(['Product']); or using the cli.
    ::where("name = ?",[$_POST['name']])
    ->toList();
```

Table of contents
=================

[](#table-of-contents)

- [Database Access Object wrapper for PHP and PDO in a single class](#database-access-object-wrapper-for-php-and-pdo-in-a-single-class)
- [Table of contents](#table-of-contents)
    - [Examples](#examples)
    - [Installation](#installation)
        - [Install (using composer)](#install-using-composer)
        - [Install (manually)](#install-manually)
    - [How to create a Connection?](#how-to-create-a-connection)
        - [OCI](#oci)
    - [CLI configuration.](#cli-configuration)
        - [Connect CLI](#connect-cli)
        - [Save](#save)
        - [Create ORM](#create-orm)
        - [Scan](#scan)
        - [(optional) detail and type](#optional-detail-and-type)
        - [Save](#save-1)
        - [Create](#create)
    - [Using the code generated](#using-the-code-generated)
    - [ORM](#orm)
        - [What is an ORM?](#what-is-an-orm)
        - [Building and installing the ORM manually](#building-and-installing-the-orm-manually)
            - [Creating the repository class](#creating-the-repository-class)
            - [Creating multiples repositories classes](#creating-multiples-repositories-classes)
            - [Creating all repositories classes](#creating-all-repositories-classes)
        - [Using the Repository class.](#using-the-repository-class)
            - [Using multiples connections](#using-multiples-connections)
        - [DDL Database Design Language](#ddl--database-design-language)
        - [Nested Operators](#nested-operators)
        - [DQL Database Query Language](#dql-database-query-language)
        - [DML Database Model Language](#dml-database-model-language)
        - [Validate the model](#validate-the-model)
        - [Recursive](#recursive)
            - [recursive()](#recursive-1)
            - [getRecursive()](#getrecursive)
            - [hasRecursive()](#hasrecursive)
    - [Benchmark (mysql, estimated)](#benchmark-mysql-estimated)
    - [Error FAQs](#error-faqs)
        - [Uncaught Error: Undefined constant eftec\_BasePdoOneRepo::COMPILEDVERSION](#uncaught-error-undefined-constant-eftec_basepdoonerepocompiledversion)
    - [Changelist](#changelist)
    - [License](#license)

Examples
--------

[](#examples)

There are some examples in the **"examples"** folder. If you want to run the examples, then you must change the configuration of the database.

Other example here:

- [getting started](examples/sakilatest.php)

Installation
------------

[](#installation)

This library requires PHP 7.4 and higher, and it requires the extension PDO and the extension PDO-MYSQL (Mysql), PDO-SQLSRV (sql server) or PDO-OCI (Oracle)

### Install (using composer)

[](#install-using-composer)

Edit **composer.json** the next requirement, then update composer.

```
  {
      "require": {
        "eftec/PdoOneORM": "^1.0"
      }
  }
```

or install it via cli using

> composer require eftec/PdoOneORM

### Install (manually)

[](#install-manually)

Just download the folder lib from the library and put in your folder project. Then you must include all the files included on it.

How to create a Connection?
---------------------------

[](#how-to-create-a-connection)

Create an instance of the class PdoOne as follows. Then, you can open the connection using the method connect() or open()

```
use eftec\PdoOneORM;
// mysql
$dao=new PdoOneORM("mysql","127.0.0.1","root","abc.123","sakila","");
$conn->logLevel=3; // it is for debug purpose and it works to find problems.
$dao->connect();

// sql server 10.0.0.1\instance or (local)\instance or machinename\instance or machine (default instance)
$dao=new PdoOneORM("sqlsrv","(local)\sqlexpress","sa","abc.123","sakila","");
$conn->logLevel=3; // it is for debug purpose and it works to find problems.
$dao->connect();

// test (mockup)
$dao=new PdoOneORM("test","anyy","any","any","any","");
$dao->connect();

// oci (oracle) ez-connect. Remember that you must have installed Oracle Instant client and add it to the path.

$cs='(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = instancia1)))';
$dao=new PdoOneORM("oci",$cs,"sa","abc.123"); // oracle uses the user as the schema
$conn->logLevel=3; // it is for debug purpose and it works to find problems.
$dao->connect();

// oci (oracle) tsnnames (the environment variables TNS_ADMIN and PATH must be correctly configured), also tnsnames.ora must exists.
$cs='instancia1';
$dao=new PdoOneORM("oci",$cs,"sa","abc.123"); // oracle uses the user as the schema
$conn->logLevel=3; // it is for debug purpose and it works to find problems.
$dao->connect();
```

where

> $dao=new PdoOneORM("mysql","127.0.0.1","root","abc.123","sakila","");

- "**mysql**" is the MySQL database. It also allows sqlsrv (for sql server) or "oci" (oracle)
- **127.0.0.1** is the server where is the database.
- **root** is the user
- **abc.123** is the password of the user root.
- **sakila** is the database used.
- "" (optional) it could be a log file, such as c:\\temp\\log.txt

### OCI

[](#oci)

- Windows installation. Add the Oracle Instant client to the path and try to run from it.
    - If it fails, the copy the oracle bin folder (instant client) into the apache folder.

CLI configuration.
------------------

[](#cli-configuration)

The command line interface is important to generate the code needed. You can also generate code using PHP code but using the CLI is more straightforward. In the command line, runs the next command:

```
.\vendor\bin\pdoonecli.bat  # windows
./vendor/bin/pdoonecli  # linux bash
```

And it will show the next screen

[![img1.png](docs/img1.png)](docs/img1.png)

> Note: you can use the arrow keys and the TAB key for autocomplete values.

### Connect CLI

[](#connect-cli)

Selection the option connect, then configure and configure your database. In the next example we will configure a MySql database (schema sakila).

> You can't configure a database if your PHP installation is not using the correct drivers (including the PDO Drivers)

[![img1.png](docs/img2.png)](docs/img2.png)

If the connect was done successfully (it shows **ok**), then you can continue.

### Save

[](#save)

Once configured, you can save your configuration (**save**). [![img3.png](docs/img3.png)](docs/img3.png)

Later, if you want to reconfigure, you can load your configuration file using the option **load**.

The configuration file is stored in a PHP file with extension .config.php

Example:

```
