Recommend this page to a friend! |
Classes of Jorge Castro | PDO One ORM | README.md | Download |
|
DownloadDatabase Access Object wrapper for PHP and PDO in a single classPdoOneORM. 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) Turn this
into this using the ORM.
Table of contents<!-- TOC --> * Database Access Object wrapper for PHP and PDO in a single class * Table of contents * Examples * Installation
* ORM
* Benchmark (mysql, estimated) * Error FAQs
* Changelist * License <!-- TOC --> ExamplesThere 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: InstallationThis library requires PHP 7.1 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","");
OCI
ORMThis 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 ORMThere 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 classesIn this example, we have two classes, messages and users
It will generate the next classes:
Creating all repositories classesWe 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 connectionsNote: 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:
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 LanguageThe 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 OperatorsThe nested operators are methods that should be in between of our chain of methods. > ClassRepo::op()::where()::finalop() is ? > > ClassRepo::op()::op()::where() will left 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 LanguageWe 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<br />$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)<br />$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); <br />$data=TableNameRepo::where('a1=?',[$value])::count(); | DML Database Model LanguageThe 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 modelIt 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).
RecursiveA 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 FAQsUncaught Error: Undefined constant eftec\_BasePdoOneRepo::COMPILEDVERSIONIt means that you are updated PdoOne, and you are using one class generated by the ORM. This class must be re-generated. ChangelistIn 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)
LicenseCopyright Jorge Castro Castillo 2023. Dual license, commercial and LGPL-3.0 |