SlideShare a Scribd company logo
Databases and Doctrine
Doctrine ORM
● aims to let you map objects to a relational
database (such as MySQL, PostgreSQL or
Microsoft SQL)
A Simple Example: A Product
● $ php app/console generate:bundle
--namespace=Acme/StoreBundle
Configuring the Database
● configure your database connection information
in an app/config/parameters.yml file
parameters defined in that file are
referenced by the main
configuration file:
Now that Doctrine knows about your
database, you can have it create the
database for you:
● $ php app/console 
doctrine:database:create
Or destroy the DB:
● $ php app/console 
doctrine:database:drop ­­force
Creating your first table...
Creating an Entity Class
● Create this class inside the Entity directory of
your AcmeStoreBundle:
Add Mapping Information
● Doctrine allows you to persist entire objects to
the database and fetch entire objects out of the
database.
Databases and doctrine
Doctrine Field Types Reference
●     Strings
●         string (used for shorter strings)
●         text (used for larger strings)
●     Numbers
●         integer
●         smallint
●         bigint
●         decimal
●         float
●     Dates and Times (use a DateTime object for these fields in PHP)
●         date
●         time
●         datetime
●     Other Types
●         boolean
●         object (serialized and stored in a CLOB field)
●         array (serialized and stored in a CLOB field)
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-
mapping.html#doctrine-mapping-types
Generating Getters and Setters
● $ php app/console 
doctrine:generate:entities 
Acme/StoreBundle/Entity/Product
● You can also generate all known entities (i.e. any PHP
class with Doctrine mapping information) of a bundle or
an entire namespace:
● $ php app/console 
doctrine:generate:entities AcmeStoreBundle
● $ php app/console 
doctrine:generate:entities Acme
Creating the Database
Tables/Schema
● $ php app/console 
doctrine:schema:update ­­force
Persisting Objects to the Database
Fetching Objects from the Database
helpful methods:
findBy and findOneBy methods to
easily fetch objects based on
multiple conditions
Updating an Object
Deleting an Object
Querying for Objects
Querying for Objects with DQL
● The getResult() method returns an array of
results.
querying for just one object
Setting Parameters
Using Doctrine's Query Builder
Mapping Custom Repository Classes
● a custom repository class for your entity and
add methods with your query logic there.
Creating Custom Repository Classes
Using Custom Repository Classes
● When using a custom repository class, you still
have access to the default finder methods such
as find() and findAll().
Entity Relationships/Associations
Category
ProductProduct Product
Relationship Mapping Metadata
since each Product class can relate to exactly one
Category object
● Then, generate the setter(s) and getter(s)
●
● tell Doctrine to add the new category table, and
product.category_id column, and new foreign key:
Databases and doctrine
Saving Related Entities
Fetching Related Objects
Lazy Loading
Joining to Related Records
Field Options
More on Association Mapping
One-To-One, Unidirectional
One-To-One, Bidirectional
One-To-One, Self-referencing
One-To-Many, Unidirectional with Join Table
Many-To-One, Unidirectional
One-To-Many, Bidirectional
One-To-Many, Self-referencing
Many-To-Many, Unidirectional
Many-To-Many, Bidirectional
Many-To-Many, Self-referencing
Mapping Defaults
● defaults for a join column in a one-to-one/many-to-one
association is as follows:
● name: "<fieldname>_id"
● referencedColumnName: "id"
Databases and doctrine
For more info:
http://www.doctrine-project.org/

More Related Content

PPTX
PPT
Core & advanced java classes in mumbai
PPT
JahiaOne - Jahia 7, The External Data Provider
PPTX
Data Structure - Elementary Data Organization
PPTX
Ian 2014.10.24 weekly report
PPT
Abstract data types
PPT
1 1 5 Clases
 
Core & advanced java classes in mumbai
JahiaOne - Jahia 7, The External Data Provider
Data Structure - Elementary Data Organization
Ian 2014.10.24 weekly report
Abstract data types
1 1 5 Clases
 

What's hot (20)

PDF
PPT
March 2012 HUG: JuteRC compiler
PPT
Elementary data organisation
PPTX
Array c programming
PDF
Algorithm and Data Structure - Array and Struct
PPT
Basic terminologies
PPT
Abstract data types
PPTX
Session 17 - Collections - Lists, Sets
PDF
Lecture 4 - Object Interaction and Collections
PPTX
Slide 1.-datastructure
PPT
Basics of data structure
PPTX
Java Arrays and DateTime Functions
PPTX
Dsa unit 1
PPTX
Mca ii dfs u-1 introduction to data structure
PDF
20130222 Data structures and manipulation in R
PPTX
Array vs array list
PPT
Data struters
PPTX
Session 20 - Collections - Maps
PPTX
introduction to Data Structure and classification
PPT
Data structures and Alogarithims
March 2012 HUG: JuteRC compiler
Elementary data organisation
Array c programming
Algorithm and Data Structure - Array and Struct
Basic terminologies
Abstract data types
Session 17 - Collections - Lists, Sets
Lecture 4 - Object Interaction and Collections
Slide 1.-datastructure
Basics of data structure
Java Arrays and DateTime Functions
Dsa unit 1
Mca ii dfs u-1 introduction to data structure
20130222 Data structures and manipulation in R
Array vs array list
Data struters
Session 20 - Collections - Maps
introduction to Data Structure and classification
Data structures and Alogarithims
Ad

Similar to Databases and doctrine (20)

PDF
Doctrine Php Object Relational Mapper
PDF
Doctrine and NoSQL
PDF
Doctrine for NoSQL
PDF
ZendCon2010 The Doctrine Project
PDF
Jooctrine - Doctrine ORM in Joomla!
PDF
ZendCon2010 Doctrine MongoDB ODM
PDF
Doctrine with Symfony - SymfonyCon 2019
PDF
Symfony2 and Doctrine2 Integration
KEY
Object Relational Mapping in PHP
PDF
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
PDF
Symfony Day 2010 Doctrine MongoDB ODM
PDF
ITB2017 - Slaying the ORM dragons with cborm
PPT
Doctrine 2 - Introduction
PDF
Killing Shark-Riding Dinosaurs with ORM
PDF
Doctrine Project
PDF
Laravel doctrine
PDF
Libertyvasion2010
PDF
Doctrine MongoDB ODM (PDXPHP)
PDF
Zend Framework 1 + Doctrine 2
Doctrine Php Object Relational Mapper
Doctrine and NoSQL
Doctrine for NoSQL
ZendCon2010 The Doctrine Project
Jooctrine - Doctrine ORM in Joomla!
ZendCon2010 Doctrine MongoDB ODM
Doctrine with Symfony - SymfonyCon 2019
Symfony2 and Doctrine2 Integration
Object Relational Mapping in PHP
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
Symfony Day 2010 Doctrine MongoDB ODM
ITB2017 - Slaying the ORM dragons with cborm
Doctrine 2 - Introduction
Killing Shark-Riding Dinosaurs with ORM
Doctrine Project
Laravel doctrine
Libertyvasion2010
Doctrine MongoDB ODM (PDXPHP)
Zend Framework 1 + Doctrine 2
Ad

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Spectroscopy.pptx food analysis technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
MYSQL Presentation for SQL database connectivity
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectroscopy.pptx food analysis technology
Advanced methodologies resolving dimensionality complications for autism neur...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Assigned Numbers - 2025 - Bluetooth® Document
MYSQL Presentation for SQL database connectivity
“AI and Expert System Decision Support & Business Intelligence Systems”
Unlocking AI with Model Context Protocol (MCP)
Agricultural_Statistics_at_a_Glance_2022_0.pdf
NewMind AI Weekly Chronicles - August'25-Week II
20250228 LYD VKU AI Blended-Learning.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Electronic commerce courselecture one. Pdf
A comparative analysis of optical character recognition models for extracting...
Building Integrated photovoltaic BIPV_UPV.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Databases and doctrine