Download the PHP package eftec/pdooneorm without Composer
On this page you can find all versions of the php package eftec/pdooneorm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package pdooneorm
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.
PdoOneORM | Eloquent and others code-first ORM |
---|---|
Database first. You create the database and it will generate the code | Code First. You create the code and it will generate the database |
This 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 database |
Relations are created using foreign keys or manually. | Relation could create foreign keys. |
Why?
Turn this
into this using the ORM.
Table of contents
- Database Access Object wrapper for PHP and PDO in a single class
- Table of contents
- Examples
- Installation
- Install (using composer)
- Install (manually)
- How to create a Connection?
- OCI
- CLI configuration.
- Connect CLI
- Save
- Create ORM
- Scan
- (optional) detail and type
- Save
- Create
- Using the code generated
- ORM
- What is an ORM?
- Building and installing the ORM manually
- Creating the repository class
- Creating multiples repositories classes
- Creating all repositories classes
- Using the Repository class.
- Using multiples connections
- DDL Database Design Language
- Nested Operators
- DQL Database Query Language
- DML Database Model Language
- Validate the model
- Recursive
- recursive()
- getRecursive()
- hasRecursive()
- Benchmark (mysql, estimated)
- Error FAQs
- Uncaught Error: Undefined constant eftec_BasePdoOneRepo::COMPILEDVERSION
- Changelist
- License
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
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)
Edit composer.json the next requirement, then update composer.
or install it via cli using
composer require eftec/PdoOneORM
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?
Create an instance of the class PdoOne as follows. Then, you can open the connection using the method connect() or open()
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
- 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.
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:
And it will show the next screen
Note: you can use the arrow keys and the TAB key for autocomplete values.
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)
If the connect was done successfully (it shows ok), then you can continue.
Save
Once configured, you can save your configuration (save).
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:
You can use this file freely in your code, of you could simply copy and paste in your final code.
Create ORM
Once the database is configured, return to the main menu.
In the menu connect, press enter to return to the main menu. then enter repo to go to the repository menu.
It is the ORM menu:
First, we must configure the folder and the namespace where the files will be generated.
The folder and namespace must work with your composer.json configuration (autoload). If you edit your composer.json file, then you must reopen the CLI.
Example of composer.json
-
Go to the folder option
- Enter the folder that you want to store your PHP files. In this example, the folder will be examples/repo
-
Then indicates a postfix for the class.
- If the table is named Customer, and the postfix is Repo, then the files will be called:
- CustomerRepo.php
- AbstractCustomerRepo.php
- Then indicate the namespace corresponding to your composer.json (autoload) and your new folder.
- If autoload is set the namespace to to eftec\examples => folder examples/
- then the folder examples/repo => namespace eftec\examples\repo
Scan
Once the folder is set, you must scan the schema. If you do changes to the schema, then you must re-scan it again.
(optional) detail and type
Optionally you can change the detail of every table, for example how the classes are called, what column are used and to determine the type for every column.
Save
Save the configuration, so you can load it later.
Create
And finally, you can create the PHP files.
It will generate 3 kind of files.
- Abstract classes (marked with red). The abstract class is a class used by the repo class. It contains all the information of the schema. This class must not be edited manually and you can't use it directly.
- Repo classes (marked with green). The repo class is the repository class and it is the class that you can use finally. You could edit this file.
- Schema class (marked with yellow). The schema class is the class that contains information of all the schema. You mustn't edit this file or use it directly.
Reload the configuration
You can easily reload the previous configuration by running in the shell
pdooneorm --fileconnect c1 --filerepo p1
where c1 is the first configuration file (without extension), and p1 is the second configuration file.
Using the code generated
Once the code is generated, you could use the repo classes.
1) You must connect to the database once so the repo classes could work correctly. 2) Then you can use the repo classes.
ORM
This library allows creating and use it as an ORM. To use it as an ORM, you must create the classes.
What is an ORM?
An ORM transforms queries to the database in objects serializables.
Let's say the next example
You can also run using the Query Builder
What if you use the same table over and over. You can generate a new class called CustomerRepo and calls the code as
While it is simple, but it also hides part of the implementation. It could hurt the performance a bit, but it adds more simplicity and consistency.
Building and installing the ORM manually
There are several ways to generate a Repository code, it is possible to generate a code using the CLI, the GUI or using the next code:
The code generated looks like this one
Creating the repository class
This method is not recommended. Uses the method to create multiple classes.
There are several ways to create a class, you could use the UI, the CLI or directly via code.
It is an example to create our repository class
It will build our Repository class.
It will generate the next class
Creating multiples repositories classes
In this example, we have two classes, messages and users
It will generate the next classes:
- Abstract Classes are classes with all the definitions of the tables, indexes and such. They contain the whole definition of a class.
- This class should be rebuilded if the table changes. How? You must run the method generateAllClasses() again.
- Repo Classes are classes that works as a placeholder of the Abstract class. These classes are safe for edit, so we could add our own methods and logic.
- Note: if you run generateAllClasses() again, then those classes are not touched unless we force it (argument $forced) or we delete those files.
- Base Class is a unique class (per schema) where it contains the definition of all the tables and the relations between them.
- This class should be rebuilt if the table changes. How? You must run the method generateAllClasses() again.
Creating all repositories classes
We could automate even further
Using the Repository class.
For started, the library must know to know where to connect, so you must set an instance of the PdoOne and there are 3 ways to instance it.
The repository class is smart, and it does the next operation:
If the Repository base doesn't have a connection, then it will try to use the latest connection available.
The easiest way is to create an instance of PdoOne();
You could also do it by creating a root function called pdoOneORM()
Or creating a global variable called $pdoOne
Or injecting the instance into the class using the static method Class::setPdoOne()
Using multiples connections
Note: If you are using multiples connections, then you must use the method RepoClass::setPdoOne() and it injects the connection inside the Repository Base.
Every repository base could hold only one connection at the same time
Example:
- BaseAlpha (Base class)
- Table1AlphaRepo (Repository class)
- Table2AlphaRepo (Repository class)
- BaseBeta (Base class)
- Table1BetaRepo (Repository class)
- Table2BetaRepo (Repository class)
What if you want to use the same base for different connections? You can't. However, you could copy the files and create two different bases and repositories (or you could generate a code to create a new base and repository classes), then you can use multiples connections.
DDL Database Design Language
The next commands usually are executed alone (not in a chain of methods)
Method | Description | Example |
---|---|---|
createTable() | Creates the table and indexes using the definition inside the Repo | TablaParentRepo::createTable(); |
createForeignKeys() | Create all foreign keys of the table | TablaParentRepo::createForeignKeys(); |
dropTable() | Drop the table | TablaParentRepo::dropTable(); |
truncate() | Truncate the table | TablaParentRepo::truncate(); |
validTable() | Validate if the table hasn't changed | $ok=TablaParentRepo::validTable(); |
Nested Operators
The nested operators are methods that should be in between of our chain of methods.
ClassRepo::op()::where()::finalop() is ✅
ClassRepo::op()::op()::where() will leave the chain open ❌
For example:
Method | Description | Example |
---|---|---|
where() | It adds a where to the chain | TablaParentRepo::where() |
order() | It adds a order by to the chain | TablaParentRepo::order() |
group() | it adds a group by to the chain | TablaParentRepo::group() |
limit() | It limits the results | TablaParentRepo::limit() |
page() | Its similar to limit but it uses page | TablaParentRepo::page() |
innerjoin() | It adds a inner join to the query | TablaParentRepo::innerjoin() |
left() | It adds a left join to the query | TablaParentRepo::left() |
right() | It adds a right join to the query | TablaParentRepo::right() |
DQL Database Query Language
We have different methods to generate a DQL (query) command in our database.
If the operation fails, they return a FALSE, and they could trigger an exception.
The next methods should be at the end of the chain. Examples:
ClassRepo::op()::op()::toList() is ✅
ClassRepo::op()::toList()::op() will trigger an exception ❌
Command | Description | Example |
---|---|---|
toList() | Returns an array of elements | $data=TableNameRepo::toList(); // select from tablerepo $data=TableNameRepo::where('a1=?',[$value])::toList(); // select from tablerepo where a1=$value |
first() | Returns a simple row | $data=TableNameRepo::first($pk); // select * from tablerepo where pk=$pk (it always returns 1 or zero values) $data=TableNameRepo::where('a1=?',[$value])::first(); // it returns the first value (or false if not found) |
exist() | Returns true if a primary key exists | $data=TableNameRepo::exist($pk); // returns true if the object exists. |
count() | Returns the number of rows in a query | $data=TableNameRepo::count($conditions); $data=TableNameRepo::where('a1=?',[$value])::count(); |
DML Database Model Language
The next methods allow inserting,update or delete values in the database.
Method | Description | Example |
---|---|---|
insert | It inserts a value into the database. It could return an identity | $identity=TablaParentRepo::insert($obj); |
update | It updates a value into the database. | TablaParentRepo::update($obj); |
delete | It deletes a value from the database. | TablaParentRepo::delete($obj); |
deletebyId | It deletes a value (using the primary key as condition) from the database. | TablaParentRepo::deleteById($pk); |
Validate the model
It is possible to validate the model. The model is validated using the information of the database, using the type of the column, the length, if the value allows null and if it is identity (auto numeric).
Recursive
A recursive array is an array of strings with values that it could be read or obtained or compared. For example, to join a table conditionally. PdoOne does not use it directly but _BasePdoOneRepo uses it (_BasePdoOneRepo is a class used when we generate a repository service class automatically).
Example
recursive()
It sets a recursive array.
This value is resets each time a chain methods ends.
getRecursive()
It gets the recursive array.
hasRecursive()
It returns true if recursive has some needle.
If $this->recursive is ['*'] then it always returns true.
Benchmark (mysql, estimated)
Library | Insert | findPk | hydrate | with | time |
---|---|---|---|---|---|
PDO | 671 | 60 | 278 | 887 | 3,74 |
PdoOne | 774 | 63 | 292 | 903 | 4,73 |
LessQL | 1413 | 133 | 539 | 825 | 5,984 |
YiiM | 2260 | 127 | 446 | 1516 | 8,415 |
YiiMWithCache | 1925 | 122 | 421 | 1547 | 7,854 |
Yii2M | 4344 | 208 | 632 | 1165 | 11,968 |
Yii2MArrayHydrate | 4114 | 213 | 531 | 1073 | 11,22 |
Yii2MScalarHydrate | 4150 | 198 | 421 | 516 | 9,537 |
Propel20 | 2507 | 123 | 1373 | 1960 | 11,781 |
Propel20WithCache | 1519 | 68 | 1045 | 1454 | 8,228 |
Propel20FormatOnDemand | 1501 | 72 | 994 | 1423 | 8,228 |
DoctrineM | 2119 | 250 | 1592 | 1258 | 18,139 |
DoctrineMWithCache | 2084 | 243 | 1634 | 1155 | 17,952 |
DoctrineMArrayHydrate | 2137 | 240 | 1230 | 877 | 16,83 |
DoctrineMScalarHydrate | 2084 | 392 | 1542 | 939 | 18,887 |
DoctrineMWithoutProxies | 2119 | 252 | 1432 | 1960 | 19,822 |
Eloquent | 3691 | 228 | 708 | 1413 | 12,155 |
PdoOne adds a bit of ovehead over PDO, however it is simple a wrapper to pdo.
Error FAQs
Uncaught Error: Undefined constant eftec_BasePdoOneRepo::COMPILEDVERSION
It means that you are updated PdoOne, and you are using one class generated by the ORM. This class must be re-generated.
Changelist
In a nutshell:
Every major version means that it could break old code. I.e. 1.0 -> 2.0
Every minor version means that it adds a new functionality i.e. 1.5 -> 1.6 (new methods)
Every decimal version means that it patches/fixes/refactoring a previous functionality i.e. 1.5.0 -> 1.5.1 (fix)
- 2.5 2024-12-30
- Compatible with PHP 8.4
- 2.4 2024-09-06
- updated documentation and in the use of the CLI.
- 2.3 2024-08-02
- [fixed] fixed a bug when the database is called with a minus symbol.
- [updated] Updated dependencies
- 2.2 2024-06-07
- Update phpdoc using markdown without "php" because PHPStorm is not compatible with it.
- 2.1 2024-03-02
- Updating dependency to PHP 7.4. The extended support of PHP 7.2 ended 3 years ago.
- Added more type hinting in the code.
- 2.0 2023-12-13
- Repo classes: Constant fields are now regular fields.
- Why? Constants are a bit slow, but it also lacks of flexibility.
- Example: (prev) CustomerRepo::TABLE, (now) CustomerREPO::$TABLE
- Repositories classes must be regenerated.`
- 1.3.1 2023-11-13
- Update some templates
- It shows the table source when update() fails
- 1.3 2023-09-02
- [PdoOneORMCli] 1.9 fixed a bug not scanning.
- Update composer.json dependency.
- 1.2.2 2023-08-11
- [PdoOneORMCli] 1.8.2 fixed a bug not scanning.
- Update dependencies to eftec/pdoone 4.3
- 1.2.1 2023-05-21
- [PdoOneORMCli] 1.8.1 fixed a small bug in "configure per type"
- 1.2 2023-04-7
- [PdoOneORM] update for compatibility with PdoOne 4.2 and higher
- [composer.json] PHPUnit reduced to 8.5 (PHP 7.2 compatible)
- [PdoOneORMCli] 1.8 now configuration files are stored as PHP file instead of a json. Why? It is more flexible.
- 1.1 2023-03-21
- [PdoOneORMCli] 1.7 CLI menu updated using the new functionality of the menu from CliOne 1.26.1
- 1.0 2023-03-11
- First version. This version is split from library PdoOne.
License
Copyright Jorge Castro Castillo 2023. Dual license, commercial and LGPL-3.0
All versions of pdooneorm with dependencies
eftec/clione Version ^1.35
eftec/pdoone Version ^4.12.1
ext-pdo Version *
ext-json Version *