Today we are going to discuss a very popular database encapsulation wrapper framework ( used to be known as Object Relational Mapping ). This has been evolved on Apr 2006, and name was zYne-. Finally Doctrine released with its own name and with 1.0.0 stable release on Sep 1, 2008.
Now what is Doctrine, if we want to discuss on it, below are the best points to describe it:
You can clone the sample code from my GitHub repo. You are done cloning right. Let’s setup by running :
If you will now inspect you command prompt (I prefer git bash) and composer.json file you will notice, I did a modification where all packages will be downloaded. The default name is "vendor" as per Composer's Dependency Management, but I modified it to "packages":
Now we will run a command to generate the schema (table structure):
but before that, I installed some config files, I took a snapshot(you will find the same in my Github repo):
Now we can use our newly created entities to store user in database. To do this,
Now what is Doctrine, if we want to discuss on it, below are the best points to describe it:
- It's an Object Relational Mapper framework
- Persistence and transnational property of an entity object in PHP is quite transparent
- Using this framework the database layer can be completely isolated. It uses a Data mapper pattern, so the actual business logic is completely isolated from Database entities persistence
- The implementation is done by a specific Query Language (Doctrine Query Language), which similar to SQL but some object oriented idiom is in it
Install doctrine to your project:
You need doctrine/orm and doctrine/doctrine-bundle and those be installed via below composer commands
composer require doctrine/orm
composer require doctrine/doctrine-bundle
OR: Create a composer.json and add your project root. There is a command you know from the last discussion of composer. The require section will like:
},
"require": {
"php": ">=5.3.9",
"symfony/symfony": "2.8.17",
"doctrine/orm": "^2.4.8",
"doctrine/dbal": "<2.5",
"doctrine/doctrine-bundle": "~1.4",
},
Application Level Commands:
There are below application level commands which provided by doctrine/doctrine-bundle,
doctrine:database:create
doctrine:database:drop
doctrine:generate:entities
doctrine:mapping:import
You can clone the sample code from my GitHub repo. You are done cloning right. Let’s setup by running :
composer install -o
If you will now inspect you command prompt (I prefer git bash) and composer.json file you will notice, I did a modification where all packages will be downloaded. The default name is "vendor" as per Composer's Dependency Management, but I modified it to "packages":
Now we will run a command to generate the schema (table structure):
packages/bin/doctrine orm:schema-tool:create
but before that, I installed some config files, I took a snapshot(you will find the same in my Github repo):
What is next? Create user:
Now we can use our newly created entities to store user in database. To do this,
- we will create a script with data persist logic
- with this we can create user
- we need to run: php create_user.php <nameofuser>
Code is like:
Retrieve data:
To retrieve single user from database, doctrine “find” function is used
It returns an object of user entity class
- To retrieve single user with specific field from database, doctrine “findOneBy” function is used
- It returns an object of user entity class
- To find all entities and get relational table data dql (doctrine query language) is used
- Relational fields are joined with mapped entity field name
- Any mapped field loaded as whole entity and any field can be retrieved
Pagination:
- DQL (Doctrine Query Language) can also be used with pagination
- with paginator first and max result can be provided
- Below steps are executed with pagination
- perform a Count query using `DISTINCT` keyword
- perform a Limit Subquery with `DISTINCT` to find all ids of the entity in from on the current page.
- perform a WHERE IN query to get all results for the current page
That's all folks for now. Stay tuned for the next topic. Feel free to add your questions and feedback section.
Comments
Post a Comment
Thank you for leaving your valuable feedback, it's important, however, I'd like to review once more. Allow me, and stay tuned