Skip to content

Commit 4b2ec24

Browse files
Moving sidebar below introduction of related command
1 parent e1be4d1 commit 4b2ec24

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

book/doctrine.rst

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ persist it to the database and fetch it back out.
3434

3535
If you want to follow along with the example in this chapter, create
3636
an ``AcmeStoreBundle`` via:
37-
37+
3838
.. code-block:: bash
39-
39+
4040
$ php app/console generate:bundle --namespace=Acme/StoreBundle
4141
4242
Configuring the Database
@@ -61,9 +61,9 @@ information. By convention, this information is usually configured in an
6161
Defining the configuration via ``parameters.ini`` is just a convention.
6262
The parameters defined in that file are referenced by the main configuration
6363
file when setting up Doctrine:
64-
64+
6565
.. code-block:: yaml
66-
66+
6767
# app/config/config.yml
6868
doctrine:
6969
dbal:
@@ -72,13 +72,20 @@ information. By convention, this information is usually configured in an
7272
dbname: %database_name%
7373
user: %database_user%
7474
password: %database_password%
75-
75+
7676
By separating the database information into a separate file, you can
7777
easily keep different versions of the file on each server. You can also
7878
easily store database configuration (or any sensitive information) outside
7979
of your project, like inside your Apache configuration, for example. For
8080
more information, see :doc:`/cookbook/configuration/external_parameters`.
8181

82+
Now that Doctrine knows about your database, you can have it create the database
83+
for you:
84+
85+
.. code-block:: bash
86+
87+
$ php app/console doctrine:database:create
88+
8289
.. sidebar:: Setting Up The Database
8390

8491
One mistake even seasoned developers make when starting a Symfony2 project
@@ -100,17 +107,10 @@ information. By convention, this information is usually configured in an
100107
your configuration file (typically ``my.cnf``):
101108

102109
.. code-block:: ini
103-
110+
104111
[mysqld]
105112
collation-server = utf8_general_ci
106-
character-set-server = utf8
107-
108-
Now that Doctrine knows about your database, you can have it create the database
109-
for you:
110-
111-
.. code-block:: bash
112-
113-
$ php app/console doctrine:database:create
113+
character-set-server = utf8
114114
115115
Creating an Entity Class
116116
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -141,9 +141,9 @@ just a simple PHP class.
141141

142142
Once you learn the concepts behind Doctrine, you can have Doctrine create
143143
this entity class for you:
144-
144+
145145
.. code-block:: bash
146-
146+
147147
$ php app/console doctrine:generate:entity --entity="AcmeStoreBundle:Product" --fields="name:string(255) price:float description:text"
148148
149149
.. index::
@@ -440,9 +440,9 @@ Let's walk through this example:
440440
In fact, since Doctrine is aware of all your managed entities, when you
441441
call the ``flush()`` method, it calculates an overall changeset and executes
442442
the most efficient query/queries possible. For example, if you persist a
443-
total of 100 ``Product`` objects and then subsequently call ``flush()``,
444-
Doctrine will create a *single* prepared statement and re-use it for each
445-
insert. This pattern is called *Unit of Work*, and it's used because it's
443+
total of 100 ``Product`` objects and then subsequently call ``flush()``,
444+
Doctrine will create a *single* prepared statement and re-use it for each
445+
insert. This pattern is called *Unit of Work*, and it's used because it's
446446
fast and efficient.
447447

448448
When creating or updating objects, the workflow is always the same. In the
@@ -467,7 +467,7 @@ on its ``id`` value::
467467
$product = $this->getDoctrine()
468468
->getRepository('AcmeStoreBundle:Product')
469469
->find($id);
470-
470+
471471
if (!$product) {
472472
throw $this->createNotFoundException('No product found for id '.$id);
473473
}
@@ -589,7 +589,7 @@ You've already seen how the repository object allows you to run basic queries
589589
without any work::
590590

591591
$repository->find($id);
592-
592+
593593
$repository->findOneByName('Foo');
594594

595595
Of course, Doctrine also allows you to write more complex queries using the
@@ -611,7 +611,7 @@ a controller, do the following::
611611
$query = $em->createQuery(
612612
'SELECT p FROM AcmeStoreBundle:Product p WHERE p.price > :price ORDER BY p.price ASC'
613613
)->setParameter('price', '19.99');
614-
614+
615615
$products = $query->getResult();
616616

617617
If you're comfortable with SQL, then DQL should feel very natural. The biggest
@@ -632,10 +632,10 @@ for just one object, you can use the ``getSingleResult()`` method instead::
632632
need to wrap it in a try-catch block and ensure that only one result is
633633
returned (if you're querying on something that could feasibly return
634634
more than one result)::
635-
635+
636636
$query = $em->createQuery('SELECT ...')
637637
->setMaxResults(1);
638-
638+
639639
try {
640640
$product = $query->getSingleResult();
641641
} catch (\Doctrine\Orm\NoResultException $e) {
@@ -653,7 +653,7 @@ covered later), group, etc. For more information, see the official Doctrine
653653
Take note of the ``setParameter()`` method. When working with Doctrine,
654654
it's always a good idea to set any external values as "placeholders",
655655
which was done in the above query:
656-
656+
657657
.. code-block:: text
658658
659659
... WHERE p.price > :price ...
@@ -689,7 +689,7 @@ type the method names. From inside a controller::
689689
->setParameter('price', '19.99')
690690
->orderBy('p.price', 'ASC')
691691
->getQuery();
692-
692+
693693
$products = $query->getResult();
694694

695695
The ``QueryBuilder`` object contains every method necessary to build your
@@ -824,16 +824,16 @@ To relate the ``Category`` and ``Product`` entities, start by creating a
824824
825825
// ...
826826
use Doctrine\Common\Collections\ArrayCollection;
827-
827+
828828
class Category
829829
{
830830
// ...
831-
831+
832832
/**
833833
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
834834
*/
835835
protected $products;
836-
836+
837837
public function __construct()
838838
{
839839
$this->products = new ArrayCollection();
@@ -870,7 +870,7 @@ makes sense in the application for each ``Category`` to hold an array of
870870
.. tip::
871871

872872
The targetEntity value in the decorator used above can reference any entity
873-
with a valid namespace, not just entities defined in the same class. To
873+
with a valid namespace, not just entities defined in the same class. To
874874
relate to an entity defined in a different class or bundle, enter a full
875875
namespace as the targetEntity.
876876

@@ -884,11 +884,11 @@ object, you'll want to add a ``$category`` property to the ``Product`` class:
884884
// src/Acme/StoreBundle/Entity/Product.php
885885
886886
// ...
887-
887+
888888
class Product
889889
{
890890
// ...
891-
891+
892892
/**
893893
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
894894
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
@@ -970,18 +970,18 @@ Now, let's see the code in action. Imagine you're inside a controller::
970970
{
971971
$category = new Category();
972972
$category->setName('Main Products');
973-
973+
974974
$product = new Product();
975975
$product->setName('Foo');
976976
$product->setPrice(19.99);
977977
// relate this product to the category
978978
$product->setCategory($category);
979-
979+
980980
$em = $this->getDoctrine()->getEntityManager();
981981
$em->persist($category);
982982
$em->persist($product);
983983
$em->flush();
984-
984+
985985
return new Response(
986986
'Created product id: '.$product->getId().' and category id: '.$category->getId()
987987
);
@@ -1007,7 +1007,7 @@ did before. First, fetch a ``$product`` object and then access its related
10071007
->find($id);
10081008

10091009
$categoryName = $product->getCategory()->getName();
1010-
1010+
10111011
// ...
10121012
}
10131013

@@ -1034,7 +1034,7 @@ You can also query in the other direction::
10341034
->find($id);
10351035

10361036
$products = $category->getProducts();
1037-
1037+
10381038
// ...
10391039
}
10401040

@@ -1049,7 +1049,7 @@ to the given ``Category`` object via their ``category_id`` value.
10491049
This "lazy loading" is possible because, when necessary, Doctrine returns
10501050
a "proxy" object in place of the true object. Look again at the above
10511051
example::
1052-
1052+
10531053
$product = $this->getDoctrine()
10541054
->getRepository('AcmeStoreBundle:Product')
10551055
->find($id);
@@ -1115,9 +1115,9 @@ object and its related ``Category`` with just one query::
11151115
->findOneByIdJoinedToCategory($id);
11161116

11171117
$category = $product->getCategory();
1118-
1118+
11191119
// ...
1120-
}
1120+
}
11211121

11221122
More Information on Associations
11231123
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1233,7 +1233,7 @@ in general, see Doctrine's `Lifecycle Events documentation`_
12331233
callbacks should be simple methods that are concerned with internally
12341234
transforming data in the entity (e.g. setting a created/updated field,
12351235
generating a slug value).
1236-
1236+
12371237
If you need to do some heavier lifting - like perform logging or send
12381238
an email - you should register an external class as an event listener
12391239
or subscriber and give it access to whatever resources you need. For
@@ -1300,11 +1300,11 @@ and ``nullable``. Take a few examples:
13001300
/**
13011301
* A string field with length 255 that cannot be null
13021302
* (reflecting the default values for the "type", "length" and *nullable* options)
1303-
*
1303+
*
13041304
* @ORM\Column()
13051305
*/
13061306
protected $name;
1307-
1307+
13081308
/**
13091309
* A string field of length 150 that persists to an "email_address" column
13101310
* and has a unique index.
@@ -1364,9 +1364,9 @@ Some notable or interesting tasks include:
13641364
* ``doctrine:ensure-production-settings`` - checks to see if the current
13651365
environment is configured efficiently for production. This should always
13661366
be run in the ``prod`` environment:
1367-
1367+
13681368
.. code-block:: bash
1369-
1369+
13701370
$ php app/console doctrine:ensure-production-settings --env=prod
13711371
13721372
* ``doctrine:mapping:import`` - allows Doctrine to introspect an existing

0 commit comments

Comments
 (0)