SlideShare une entreprise Scribd logo
1  sur  51
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.
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:
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"
For more info:
http://www.doctrine-project.org/

Contenu connexe

Tendances

Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisation
Muzamil Hussain
 
20130222 Data structures and manipulation in R
20130222 Data structures and manipulation in R20130222 Data structures and manipulation in R
20130222 Data structures and manipulation in R
Kazuki Yoshida
 
Data structures and Alogarithims
Data structures and AlogarithimsData structures and Alogarithims
Data structures and Alogarithims
Victor Palmar
 

Tendances (20)

Lecture1 data structure(introduction)
Lecture1 data structure(introduction)Lecture1 data structure(introduction)
Lecture1 data structure(introduction)
 
March 2012 HUG: JuteRC compiler
March 2012 HUG: JuteRC compilerMarch 2012 HUG: JuteRC compiler
March 2012 HUG: JuteRC compiler
 
Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisation
 
Array c programming
Array c programmingArray c programming
Array c programming
 
Algorithm and Data Structure - Array and Struct
Algorithm and Data Structure - Array and StructAlgorithm and Data Structure - Array and Struct
Algorithm and Data Structure - Array and Struct
 
Basic terminologies
Basic terminologiesBasic terminologies
Basic terminologies
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, Sets
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
Slide 1.-datastructure
Slide 1.-datastructureSlide 1.-datastructure
Slide 1.-datastructure
 
Basics of data structure
Basics of data structureBasics of data structure
Basics of data structure
 
Java Arrays and DateTime Functions
Java Arrays and DateTime FunctionsJava Arrays and DateTime Functions
Java Arrays and DateTime Functions
 
Dsa unit 1
Dsa unit 1Dsa unit 1
Dsa unit 1
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
20130222 Data structures and manipulation in R
20130222 Data structures and manipulation in R20130222 Data structures and manipulation in R
20130222 Data structures and manipulation in R
 
Array vs array list
Array vs array listArray vs array list
Array vs array list
 
Data struters
Data strutersData struters
Data struters
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - Maps
 
introduction to Data Structure and classification
 introduction to Data Structure and classification introduction to Data Structure and classification
introduction to Data Structure and classification
 
Data structures and Alogarithims
Data structures and AlogarithimsData structures and Alogarithims
Data structures and Alogarithims
 

Similaire à Databases and doctrine

Android datastorage
Android datastorageAndroid datastorage
Android datastorage
Krazy Koder
 
ознакомления с модулем Entity api
ознакомления с модулем Entity apiознакомления с модулем Entity api
ознакомления с модулем Entity api
DrupalCamp Kyiv Рысь
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
Terry Yoast
 

Similaire à Databases and doctrine (20)

Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfit
 
La sql
La sqlLa sql
La sql
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
Android-data storage in android-chapter21
Android-data storage in android-chapter21Android-data storage in android-chapter21
Android-data storage in android-chapter21
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
 
Android datastorage
Android datastorageAndroid datastorage
Android datastorage
 
Storage 8
Storage   8Storage   8
Storage 8
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching logging
 
ознакомления с модулем Entity api
ознакомления с модулем Entity apiознакомления с модулем Entity api
ознакомления с модулем Entity api
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
 
CoreData
CoreDataCoreData
CoreData
 
02 beginning code first
02   beginning code first02   beginning code first
02 beginning code first
 
02 beginning code first
02   beginning code first02   beginning code first
02 beginning code first
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Real World MVC
Real World MVCReal World MVC
Real World MVC
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comDrupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.com
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Dernier (20)

Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Databases and doctrine