@@ -6,12 +6,13 @@ Databases and Doctrine
6
6
7
7
One of the most common and challenging tasks for any application
8
8
involves persisting and reading information to and from a database. Although
9
- the Symfony full-stack Framework doesn't integrate any ORM by default,
10
- the Symfony Standard Edition, which is the most widely used distribution,
11
- comes integrated with `Doctrine `_, a library whose sole goal is to give
12
- you powerful tools to make this easy. In this chapter, you'll learn the
13
- basic philosophy behind Doctrine and see how easy working with a database
14
- can be.
9
+ the Symfony Framework doesn't integrate any component to work with databases,
10
+ it provides tight integration with a third-party library called `Doctrine `_.
11
+ Doctrine's sole goal is to give you powerful tools to make database interactions
12
+ easy and flexible.
13
+
14
+ In this chapter, you'll learn how to start leveraging Doctrine in your Symfony projects
15
+ to give you rich database interactions.
15
16
16
17
.. note ::
17
18
@@ -609,6 +610,8 @@ repository object for an entity class via::
609
610
610
611
Once you have a repository object, you can access all sorts of helpful methods::
611
612
613
+ $repository = $this->getDoctrine()->getRepository('AppBundle:Product');
614
+
612
615
// query for a single product by its primary key (usually "id")
613
616
$product = $repository->find($productId);
614
617
@@ -630,6 +633,8 @@ Once you have a repository object, you can access all sorts of helpful methods::
630
633
You can also take advantage of the useful ``findBy `` and ``findOneBy `` methods
631
634
to easily fetch objects based on multiple conditions::
632
635
636
+ $repository = $this->getDoctrine()->getRepository('AppBundle:Product');
637
+
633
638
// query for a single product matching the given name and price
634
639
$product = $repository->findOneBy(
635
640
array('name' => 'Keyboard', 'price' => 19.99)
@@ -712,6 +717,8 @@ Querying for Objects
712
717
You've already seen how the repository object allows you to run basic queries
713
718
without any work::
714
719
720
+ $repository = $this->getDoctrine()->getRepository('AppBundle:Product');
721
+
715
722
$product = $repository->find($productId);
716
723
$product = $repository->findOneByName('Keyboard');
717
724
@@ -792,6 +799,15 @@ normal ``Query`` object, which can be used to get the result of the query.
792
799
For more information on Doctrine's Query Builder, consult Doctrine's
793
800
`Query Builder `_ documentation.
794
801
802
+ Organizing Custom Queries into Repository Classes
803
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
804
+
805
+ All the queries in the previous sections were written directly in your controller.
806
+ But for organization, Doctrine provides special repository classes that allow you
807
+ to keep all your query logic in one, central place.
808
+
809
+ see :doc: `/doctrine/repository ` for info.
810
+
795
811
Configuration
796
812
-------------
797
813
@@ -811,19 +827,23 @@ the ``length``, ``nullable`` behavior, ``name`` and other options. To see a
811
827
list of all available types and more information, see Doctrine's
812
828
`Mapping Types documentation `_.
813
829
814
- Summary
815
- -------
830
+ Relationships and Associations
831
+ ------------------------------
832
+
833
+ Doctrine provides all the functionality you need to manager database relationships
834
+ (also known as associations). For info, see :doc: `/doctrine/associations `.
835
+
836
+ Final Thoughts
837
+ --------------
816
838
817
- With Doctrine, you can focus on your objects and how they're used in your
839
+ With Doctrine, you can focus on your * objects * and how they're used in your
818
840
application and worry about database persistence second. This is because
819
841
Doctrine allows you to use any PHP object to hold your data and relies on
820
842
mapping metadata information to map an object's data to a particular database
821
843
table.
822
844
823
- And even though Doctrine revolves around a simple concept, it's incredibly
824
- powerful, allowing you to create complex queries and subscribe to events
825
- that allow you to take different actions as objects go through their persistence
826
- lifecycle.
845
+ Doctrine has a lot more complex features to learn, like relationshps, complex queries,
846
+ and event listeners.
827
847
828
848
Learn more
829
849
~~~~~~~~~~
0 commit comments