ORM (Object Relational Mapping)

ORM stands for Object-Relational Mapping. (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C#, etc.

ORM is library which helps to query the data using functions so that we not have to write database query manually. It uses objects to get the data from database.
Doctrine ORM
Example:
Configuring the Database
Before begin, configure database connection information. This information is usually configured in an app/config/parameters.yml file:
parameters:

database_host: localhost

database_name: test_project

database_user: root

database_Password: password

The parameters defined in parameters.yml are referenced by the main configuration file when setting up Doctrine:

#app/config/config.yml

doctrine:

dbal:

driver: pdo_mysql

host: ‘%database_host%’

dbname: ‘%database_name%’

user: ‘%database_user%’

password: ‘%database_password%’

Creating Database

You can create the database by running the following :

$php bin/console doctrine:database:create

Creating an Entity Class

//src/AppBundle/Entity/Product.php

Namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**

*@ORM\Entity

*@ORM\Table(name=”product”)

*/

class Product

{

/**

*@ORM\Column(type=”integer”)

*@ORM\Id

*@ORM\GeneratedValue(strategy=”AUTO”)

*/

private $id;

/**

*@ORM\Column(type=”string”, length=100)

*/

private $name;

/**

*@ORM\Column(type=”decimal”, scale=2)

*/

private $price;

/**

*@ORM\Column(type=”text”)

*/

private $description;

public function getId()

{
return $this->id;
}

public function setId($id)
{
$this->id = $id;
}

public function getName()
{
return $this->name;
}

public function setName($name)
{
$this->name = $name;
}

public function getPrice()
{
return $this->price;
}

public function setPrice($price)
{
$this->price = $price;
}

public function getDescription ()
{
return $this->description;
}

public function setDescription ($description)
{
$this-> description = $description;
}
}

Creating the Database Tables/Schema
Now we have Product class with mapping information so that Doctrine knows exactly how to persist it. Doctrine automatically create all the database tables needed for every known entity.
To do this, run:

$php bin/console doctrine:schema:update --force

Persisting Objects to the Database

use AppBundle\Entity\Product;

use Symfony\Component\HttpFoundation\Response;

use Doctrine\ORM\EntityManagerInterface;

public function createAction()

{


$em = $this->getDoctrine()->getManager();

$product = new Product();

$product->setName(‘Keyboard’);

$product->setPrice(19.99);

$product->setDescription(‘Ergonomic and stylish’);

$em->persist($product);

$em->flush();

return new Response(‘Saved new product with id’.$product->getId());

}

Fetching Objects from the Database
specific Product based on its id

Public function showAction($productId)

{

$product = $this->getDoctrine()

->getRepository(Product::class)

->find($productId);

if(!$product){

throw $this->createNotFoundException(

‘No product found for id’.$productId

);

}

}


Refference Link:
https://symfony.com/doc/3.3/doctrine.html

Leave a Reply

Your email address will not be published. Required fields are marked *