Databases and doctrine
-
Upload
glenn-guden -
Category
Technology
-
view
1.641 -
download
0
Transcript of Databases and doctrine
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
CategoryProductProductProduct
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: "_id"
referencedColumnName: "id"
For more info:
http://www.doctrine-project.org/