Skip to content

Added missing doctrine formats #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 93 additions & 10 deletions book/doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,44 @@ information. By convention, this information is usually configured in an
The parameters defined in that file are referenced by the main configuration
file when setting up Doctrine:

.. code-block:: yaml

# app/config/config.yml
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
.. configuration-block::

.. code-block:: yaml

# app/config/config.yml
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"

.. code-block:: xml

<!-- app/config/config.xml -->
<doctrine:config>
<doctrine:dbal
driver="%database_driver%"
host="%database_host%"
dbname="%database_name%"
user="%database_user%"
password="%database_password%"
>
</doctrine:config>

.. code-block:: php

// app/config/config.php
$configuration->loadFromExtension('doctrine', array(
'dbal' => array(
'driver' => '%database_driver%',
'host' => '%database_host%',
'dbname' => '%database_name%',
'user' => '%database_user%',
'password' => '%database_password%',
),
));

By separating the database information into a separate file, you can
easily keep different versions of the file on each server. You can also
Expand Down Expand Up @@ -909,6 +937,24 @@ To relate the ``Category`` and ``Product`` entities, start by creating a
mappedBy: category
# don't forget to init the collection in entity __construct() method

.. code-block:: xml

<!-- src/Acme/StoreBundle/Resources/config/doctrine/Category.orm.xml -->
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Acme\StoreBundle\Entity\Category">
<!-- ... -->
<one-to-many field="products"
target-entity="product"
mapped-by="category"
/>

<!-- don't forget to init the collection in entity __construct() method -->
</entity>
</doctrine-mapping>

First, since a ``Category`` object will relate to many ``Product`` objects,
a ``products`` array property is added to hold those ``Product`` objects.
Expand Down Expand Up @@ -966,6 +1012,28 @@ object, you'll want to add a ``$category`` property to the ``Product`` class:
name: category_id
referencedColumnName: id

.. code-block:: xml

<!-- src/Acme/StoreBundle/Resources/config/doctrine/Product.orm.xml -->
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Acme\StoreBundle\Entity\Product">
<!-- ... -->
<many-to-one field="category"
target-entity="products"
join-column="category"
>
<join-column
name="category_id"
referenced-column-name="id"
/>
</many-to-one>
</entity>
</doctrine-mapping>

Finally, now that you've added a new property to both the ``Category`` and
``Product`` classes, tell Doctrine to generate the missing getter and setter
methods for you:
Expand Down Expand Up @@ -1387,6 +1455,21 @@ and ``nullable``. Take a few examples:
length: 150
unique: true

.. code-block:: xml

<!--
A string field length 255 that cannot be null
(reflecting the default values for the "length" and *nullable* options)
type attribute is necessary in yaml definitions
-->
<field name="name" type="string" />
<field name="email"
type="string"
column="email_address"
length="150"
unique="true"
/>

.. note::

There are a few more options not listed here. For more details, see
Expand Down
8 changes: 3 additions & 5 deletions cookbook/doctrine/dbal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ To get started, configure the database connection parameters:

.. code-block:: xml

// app/config/config.xml
<!-- app/config/config.xml -->
<doctrine:config>
<doctrine:dbal
name="default"
Expand All @@ -64,9 +64,7 @@ To get started, configure the database connection parameters:
For full DBAL configuration options, see :ref:`reference-dbal-configuration`.

You can then access the Doctrine DBAL connection by accessing the
``database_connection`` service:

.. code-block:: php
``database_connection`` service::

class UserController extends Controller
{
Expand Down Expand Up @@ -186,4 +184,4 @@ mapping type:
.. _`PDO`: http://www.php.net/pdo
.. _`Doctrine`: http://www.doctrine-project.org
.. _`DBAL Documentation`: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/index.html
.. _`Custom Mapping Types`: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#custom-mapping-types
.. _`Custom Mapping Types`: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#custom-mapping-types
40 changes: 39 additions & 1 deletion cookbook/doctrine/event_listeners_subscribers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,44 @@ managers that use this connection.
</services>
</container>

.. code-block:: php

use Symfony\Component\DependencyInjection\Definition;

$container->loadFromExtension('doctrine', array(
'dbal' => array(
'default_connection' => 'default',
'connections' => array(
'default' => array(
'driver' => 'pdo_sqlite',
'memory' => true,
),
),
),
));

$container
->setDefinition(
'my.listener',
new Definition('Acme\SearchBundle\EventListener\SearchIndexer')
)
->addTag('doctrine.event_listener', array('event' => 'postPersist'))
;
$container
->setDefinition(
'my.listener2',
new Definition('Acme\SearchBundle\EventListener\SearchIndexer2')
)
->addTag('doctrine.event_listener', array('event' => 'postPersist', 'connection' => 'default'))
;
$container
->setDefinition(
'my.subscriber',
new Definition('Acme\SearchBundle\EventListener\SearchIndexerSubscriber')
)
->addTag('doctrine.event_subscriber', array('connection' => 'default'))
;

Creating the Listener Class
---------------------------

Expand All @@ -99,7 +137,7 @@ a ``postPersist`` method, which will be called when the event is thrown::

// perhaps you only want to act on some "Product" entity
if ($entity instanceof Product) {
// do something with the Product
// ... do something with the Product
}
}
}
Expand Down
95 changes: 78 additions & 17 deletions cookbook/doctrine/file_uploads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ will be covered in this cookbook entry.
Basic Setup
-----------

First, create a simple Doctrine Entity class to work with::
First, create a simple ``Doctrine`` Entity class to work with::

// src/Acme/DemoBundle/Entity/Document.php
namespace Acme\DemoBundle\Entity;
Expand Down Expand Up @@ -118,20 +118,68 @@ look like this::
}

Next, create this property on your ``Document`` class and add some validation
rules::
rules:

// src/Acme/DemoBundle/Entity/Document.php
.. configuration-block::

// ...
class Document
{
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
.. code-block:: yaml

# src/Acme/DemoBundle/Resources/config/validation.yml
Acme\DemoBundle\Entity\Document:
properties:
file:
- File:
maxSize: 6000000

.. code-block:: php-annotations

// src/Acme/DemoBundle/Entity/Document.php
namespace Acme\DemoBundle\Entity;

// ...
}
use Symfony\Component\Validator\Constraints as Assert;

class Document
{
/**
* @Assert\File(maxSize="6000000")
*/
public $file;

// ...
}

.. code-block:: xml

<!-- src/Acme/DemoBundle/Resources/config/validation.yml -->
<class name="Acme\DemoBundle\Entity\Document">
<property name="file">
<constraint name="File">
<option name="maxSize">6000000</option>
</constraint>
</property>
</class>

.. code-block:: php

// src/Acme/DemoBundle/Entity/Document.php
namespace Acme\DemoBundle\Entity;

// ...
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class Document
{
// ...

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('file', new Assert\File(array(
'maxSize' => 6000000,
)));
}
}

.. note::

Expand All @@ -141,6 +189,7 @@ rules::

The following controller shows you how to handle the entire process::

// ...
use Acme\DemoBundle\Entity\Document;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
// ...
Expand Down Expand Up @@ -176,15 +225,27 @@ The following controller shows you how to handle the entire process::

When writing the template, don't forget to set the ``enctype`` attribute:

.. code-block:: html+jinja
.. configuration-block::

.. code-block:: html+jinja

<h1>Upload File</h1>

<form action="#" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}

<input type="submit" value="Upload Document" />
</form>

.. code-block:: html+php

<h1>Upload File</h1>
<h1>Upload File</h1>

<form action="#" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<form action="#" method="post" <?php echo $view['form']->enctype($form) ?>>
<?php echo $view['form']->widget($form) ?>

<input type="submit" value="Upload Document" />
</form>
<input type="submit" value="Upload Document" />
</form>

The previous controller will automatically persist the ``Document`` entity
with the submitted name, but it will do nothing about the file and the ``path``
Expand Down
Loading