Skip to main content

An introduction to Doctrine - ORM framework - How to install & what are the usage: draft

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:

  • 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,


  1. we will create a script with data persist logic
  2. with this we can create user
  3. we need to run: php create_user.php <nameofuser>
Code is like:
create user in doctrine


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

NativeQuery

  • NativeQuery is another way to query database other than DQL
  • Passing SQL query works with NativeQuery
  • ResultSetMapping need to import

That's all folks for now. Stay tuned for the next topic. Feel free to add your questions and feedback section.

Comments

Popular posts from this blog

Layered architecture of a modern web application - Analyzing needs of CMS

  Layered architecture of a modern web application discussed nowadays, in any framework Throughout my entire career, I have searched for the word design and architecture, and I am talking about Application Design in Software Development. Today I am going to discuss one same thing with you. When design a web applications solution architecture, the aspects are commonly Performance, Scalability, Cost effective and Robust. Now performance and scalability comes with layers in an application. A web application serving a particular requirement. And a specific feature which you can break down from the requirement, obviously asks for a feature, and that is scalability. Think about a shopping cart feature, and it requires to be scalable in terms of number of end users interreacting at any specific time. We generally think about how the feature can be scalable enough and then comes the obvious factor which is layers. A layer can be a hosting solution, a framework, a third party solution like...

What is DevOps and what should I learn? - Aniruddha Banerjee - Medium

The most necessary and well-discussed topic was, why should I learn DevOps….. Most of the tech guys like me prefer to introduce myself as a Scrum specialist, Agile developer or maybe the latest term “Fullstack Developer”. Well, what I want to say that, all these impressive adjectives are basically referring to those who work within the periphery of DevOps. DevOps is Dev(development) and Ops(operation), that we all know. We know that it’s a culture, but when the question arises, what should I learn, become a DevOps cultured geek, I found myself into tons of Certifications, billions of tools, trillions of methodologies. Not a joke, but if you fall under a situation like me, when Corporate pushing me to become DevOps cultured, but I feared about Exams(it’s a childhood phobia I adopted :P), rather practically implement something worthy, or at least tried it, I became aware at the end that, this someone already had achieved, I should try something new. To study DevOps, I think, i...

SimpleSAML Installation in Windows Apache

Say you want to install SimpleSAMLphp in windows Apache/Xampp. Let me tell you what I did so far: I have downloaded latest stable version from  https://simplesamlphp.org/download  and placed the unzipped file in Apache folder, i.e. C:\Apache24\simplesamlphp directory contains composer.json. I have downloaded dependencies as well. Now when I am going to setup the vhost as shows in the site 6. Configuring Apache section as <VirtualHost *:80 > ServerName localhost DocumentRoot C:/Apache24/htdocs <VirtualHost *:80 > ServerName service.example.com DocumentRoot C:/Apache24/service.example.com Alias /simplesaml C:/Apache24/simplesamlphp/www Changed the config file Now I faced the problem: I am unable to open the Alias in browser. And running httpd.exe in browser shows error about the example.com does not exist. Later I found that I was doing some mistake... I am going to add one vhost, rather ...