Databases and doctrine

1,528 views
1,299 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,528
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Databases and doctrine

  1. 1. Databases and Doctrine
  2. 2. Doctrine ORM● aims to let you map objects to a relationaldatabase (such as MySQL, PostgreSQL orMicrosoft SQL)
  3. 3. A Simple Example: A Product● $ php app/console generate:bundle--namespace=Acme/StoreBundle
  4. 4. Configuring the Database● configure your database connection informationin an app/config/parameters.yml file
  5. 5. parameters defined in that file arereferenced by the mainconfiguration file:
  6. 6. Now that Doctrine knows about yourdatabase, you can have it create thedatabase for you:● $ php app/console doctrine:database:createOr destroy the DB:● $ php app/console doctrine:database:drop ­­force
  7. 7. Creating your first table...
  8. 8. Creating an Entity Class● Create this class inside the Entity directory ofyour AcmeStoreBundle:
  9. 9. Add Mapping Information● Doctrine allows you to persist entire objects tothe database and fetch entire objects out of thedatabase.
  10. 10. 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
  11. 11. Generating Getters and Setters● $ php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product
  12. 12. ● You can also generate all known entities (i.e. any PHPclass with Doctrine mapping information) of a bundle oran entire namespace:● $ php app/console doctrine:generate:entities AcmeStoreBundle● $ php app/console doctrine:generate:entities Acme
  13. 13. Creating the DatabaseTables/Schema● $ php app/console doctrine:schema:update ­­force
  14. 14. Persisting Objects to the Database
  15. 15. Fetching Objects from the Database
  16. 16. helpful methods:
  17. 17. findBy and findOneBy methods toeasily fetch objects based onmultiple conditions
  18. 18. Updating an Object
  19. 19. Deleting an Object
  20. 20. Querying for Objects
  21. 21. Querying for Objects with DQL● The getResult() method returns an array ofresults.
  22. 22. querying for just one object
  23. 23. Setting Parameters
  24. 24. Using Doctrines Query Builder
  25. 25. Mapping Custom Repository Classes● a custom repository class for your entity andadd methods with your query logic there.
  26. 26. Creating Custom Repository Classes
  27. 27. Using Custom Repository Classes● When using a custom repository class, you stillhave access to the default finder methods suchas find() and findAll().
  28. 28. Entity Relationships/AssociationsCategoryProductProduct Product
  29. 29. Relationship Mapping Metadata
  30. 30. since each Product class can relate to exactly oneCategory object● Then, generate the setter(s) and getter(s)●● tell Doctrine to add the new category table, andproduct.category_id column, and new foreign key:
  31. 31. Saving Related Entities
  32. 32. Fetching Related Objects
  33. 33. Lazy Loading
  34. 34. Joining to Related Records
  35. 35. Field Options
  36. 36. More on Association Mapping
  37. 37. One-To-One, Unidirectional
  38. 38. One-To-One, Bidirectional
  39. 39. One-To-One, Self-referencing
  40. 40. One-To-Many, Unidirectional with Join Table
  41. 41. Many-To-One, Unidirectional
  42. 42. One-To-Many, Bidirectional
  43. 43. One-To-Many, Self-referencing
  44. 44. Many-To-Many, Unidirectional
  45. 45. Many-To-Many, Bidirectional
  46. 46. Many-To-Many, Self-referencing
  47. 47. Mapping Defaults● defaults for a join column in a one-to-one/many-to-oneassociation is as follows:● name: "<fieldname>_id"● referencedColumnName: "id"
  48. 48. For more info:http://www.doctrine-project.org/

×