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

Dernier (20)

10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
Connecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAKConnecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAK
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
The UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, OcadoThe UX of Automation by AJ King, Senior UX Researcher, Ocado
The UX of Automation by AJ King, Senior UX Researcher, Ocado
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 

Databases and doctrine