Skip to content
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

Modernize documentation #618

Merged
merged 16 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
31 changes: 16 additions & 15 deletions Resources/doc/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Sample Configuration

.. code-block:: yaml

# app/config/config.yml
# config/packages/doctrine_mongodb.yaml
doctrine_mongodb:
connections:
default:
Expand Down Expand Up @@ -51,20 +51,20 @@ Sample Configuration
.. tip::

If each environment requires a different MongoDB connection URI, you can
define it in a separate parameter and reference it in the bundle config:
`define it as environment variable`_ and reference it in the bundle config:
SzymonKaminski marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: yaml

# app/config/parameters.yml
mongodb_server: mongodb://localhost:27017
# .env
MONGODB_URL=mongodb://localhost:27017

.. code-block:: yaml

# app/config/config.yml
# config/packages/doctrine_mongodb.yaml
doctrine_mongodb:
connections:
default:
server: "%mongodb_server%"
server: '%env(resolve:MONGODB_URL)%'

If you wish to use memcache to cache your metadata, you need to configure the
``Memcache`` instance; for example, you can do the following:
Expand Down Expand Up @@ -133,7 +133,7 @@ can control. The following configuration options exist for a mapping:
this path is relative it is assumed to be relative to the bundle root. This
only works if the name of your mapping is a bundle name. If you want to use
this option to specify absolute paths you should prefix the path with the
kernel parameters that exist in the DIC (for example %kernel.root_dir%).
kernel parameters that exist in the DIC (for example %kernel.project_dir%).
SzymonKaminski marked this conversation as resolved.
Show resolved Hide resolved

- ``prefix`` A common namespace prefix that all documents of this mapping
share. This prefix should never conflict with prefixes of other defined
Expand All @@ -154,14 +154,14 @@ can control. The following configuration options exist for a mapping:
To avoid having to configure lots of information for your mappings you should
follow these conventions:

1. Put all your documents in a directory ``Document/`` inside your bundle. For
example ``Acme/HelloBundle/Document/``.
1. Put all your documents in a directory ``Document/`` inside your project. For
example ``src/Document/``.

2. If you are using xml, yml or php mapping put all your configuration files
into the ``Resources/config/doctrine/`` directory
suffixed with mongodb.xml, mongodb.yml or mongodb.php respectively.

3. Annotations is assumed if a ``Document/`` but no
3. Annotations are assumed if a ``Document/`` but no
``Resources/config/doctrine/`` directory is found.

The following configuration shows a bunch of mapping examples:
Expand All @@ -184,7 +184,7 @@ The following configuration shows a bunch of mapping examples:
alias: BundleAlias
doctrine_extensions:
type: xml
dir: "%kernel.root_dir%/../src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents"
dir: "%kernel.project_dir%/src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents"
prefix: DoctrineExtensions\Documents\
alias: DExt

Expand All @@ -207,7 +207,7 @@ The following configuration shows a bunch of mapping examples:
<doctrine_mongodb:mapping name="MyBundle5" type="xml" dir="my-bundle-mappings-dir" alias="BundleAlias" />
<doctrine_mongodb:mapping name="doctrine_extensions"
type="xml"
dir="%kernel.root_dir%/../src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents"
dir="%kernel.project_dir%/src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents"
prefix="DoctrineExtensions\Documents\"
alias="DExt" />
</doctrine_mongodb:document-manager>
Expand Down Expand Up @@ -398,8 +398,8 @@ Using Authentication on a Database Level

MongoDB supports authentication and authorisation on a database-level. This is mandatory if you have
e.g. a publicly accessible MongoDB Server. To make use of this feature you need to configure credentials
for each of your connections. Also every connection needs a database set to authenticate against. The setting is
represented by the *authSource* `connection string <https://docs.mongodb.com/manual/reference/connection-string/#urioption.authSource>`_.
for each of your connections. Every connection needs also a database to authenticate against. The setting is
represented by the *authSource* `connection string`_.
Otherwise you will get a *auth failed* exception.

.. configuration-block::
Expand Down Expand Up @@ -442,7 +442,8 @@ Specifying a context service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The MongoDB driver supports receiving a stream context to set SSL and logging
options. This can be used to authenticate using SSL certificates. To do so, create a service that creates your logging context:
options. This can be used to authenticate using SSL certificates. To do so,
create a service that creates your logging context:

.. configuration-block::

Expand Down
8 changes: 4 additions & 4 deletions Resources/doc/console.rst
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Console Commands
================

The Doctrine2 ODM integration offers several console commands under the
The Doctrine2 ODM integration offers various console commands under the
``doctrine:mongodb`` namespace. To view the command list you can run the console
without any arguments:

.. code-block:: bash
php bin/console
A list of available command will print out, many of which start with the
``doctrine:mongodb`` prefix. You can find out more information about any
A list of available commands will be printed out, several of them start
with the ``doctrine:mongodb`` prefix. You can find out more information about any
of these commands (or any Symfony command) by running the ``help`` command.
For example, to get details about the ``doctrine:mongodb:query`` task, run:

Expand All @@ -24,4 +24,4 @@ For example, to get details about the ``doctrine:mongodb:query`` task, run:
``DoctrineFixturesBundle`` bundle installed. To learn how to do it, read
the "`DoctrineFixturesBundle`_" entry of the documentation.

.. _`DoctrineFixturesBundle`: http://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html
.. _`DoctrineFixturesBundle`: https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html
96 changes: 49 additions & 47 deletions Resources/doc/cookbook/registration_form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ Creating a Registration Form
============================

Some forms have extra fields whose values don't need to be stored in the
database. In this example, we'll create a registration form with some extra
fields and (like a "terms accepted" checkbox field) and embed the form that
actually stores the account information. We'll use MongoDB for storing the data.
database. In this example, we'll create a registration form with such
field ("terms accepted" checkbox field) and embed the form that actually
stores the account information. We'll use MongoDB for storing the data.

The simple User model
The User Model
---------------------

So, in this tutorial we begin with the model for a ``User`` document:
We begin this tutorial with the model for a ``User`` document:

.. code-block:: php

// src/Acme/AccountBundle/Document/User.php
namespace Acme\AccountBundle\Document;
// src/Document/User.php
namespace App\Document;

use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
Expand Down Expand Up @@ -72,8 +72,8 @@ So, in this tutorial we begin with the model for a ``User`` document:
}

This ``User`` document contains three fields and two of them (email and
password) should display on the form. The email property must be unique
on the database, so we've added this validation at the top of the class.
password) should be displayed in the form. The email property must be unique
in the database, so we've added this validation at the top of the class.

.. note::

Expand All @@ -87,10 +87,10 @@ Next, create the form for the ``User`` model:

.. code-block:: php

// src/Acme/AccountBundle/Form/Type/UserType.php
namespace Acme\AccountBundle\Form\Type;
// src/Form/Type/UserType.php
namespace App\Form\Type;

use Acme\AccountBundle\Document\User;
use App\Document\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
Expand All @@ -104,9 +104,9 @@ Next, create the form for the ``User`` model:
{
$builder->add('email', EmailType::class);
$builder->add('password', RepeatedType::class, [
'first_name' => 'password',
'second_name' => 'confirm',
'type' => PasswordType::class
'first_name' => 'password',
'second_name' => 'confirm',
'type' => PasswordType::class
]);
}

Expand All @@ -118,9 +118,9 @@ Next, create the form for the ``User`` model:
}
}

We just added two fields: email and password (repeated to confirm the entered
password). The ``data_class`` option tells the form the name of data class
(i.e. your ``User`` document).
We added two fields: email and password (repeated to confirm the entered
password). The ``data_class`` option tells the form the name of the class
that holds the underlying data (i.e. your ``User`` document).

.. tip::

Expand All @@ -130,32 +130,31 @@ Embedding the User form into a Registration Form
------------------------------------------------

The form that you'll use for the registration page is not the same as the
form used to simply modify the ``User`` (i.e. ``UserType``). The registration
form used to modify the ``User`` (i.e. ``UserType``). The registration
form will contain further fields like "accept the terms", whose value won't be
stored in the database.

In other words, create a second form for registration, which embeds the ``User``
form and adds the extra field needed. Start by creating a simple class which
represents the "registration":
form and adds the extra field needed:

.. code-block:: php

// src/Acme/AccountBundle/Form/Model/Registration.php
namespace Acme\AccountBundle\Form\Model;
// src/Form/Model/Registration.php
namespace App\Form\Model;

use Acme\AccountBundle\Document\User;
use App\Document\User;
use Symfony\Component\Validator\Constraints as Assert;

class Registration
{
/**
* @Assert\Type(type="Acme\AccountBundle\Document\User")
* @Assert\Type(type="App\Document\User")
*/
protected $user;

/**
* @Assert\NotBlank()
* @Assert\True()
* @Assert\IsTrue()
*/
protected $termsAccepted;

Expand All @@ -176,19 +175,19 @@ represents the "registration":

public function setTermsAccepted($termsAccepted)
{
$this->termsAccepted = (boolean)$termsAccepted;
$this->termsAccepted = (bool) $termsAccepted;
}
}

Next, create the form for this ``Registration`` model:

.. code-block:: php

// src/Acme/AccountBundle/Form/Type/RegistrationType.php
namespace Acme\AccountBundle\Form\Type;
// src/Form/Type/RegistrationType.php
namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationType extends AbstractType
Expand All @@ -200,34 +199,35 @@ Next, create the form for this ``Registration`` model:
}
}

You don't need to use special method for embedding the ``UserType`` form.
A form is a field, too - so you can add this like any other field, with the
You don't need to use any special method to embed the ``UserType`` form.
A form is a field, too - you can add it like any other field, with the
expectation that the corresponding ``user`` property will hold an instance
of the class ``UserType``.

Handling the Form Submission
----------------------------

Next, you need a controller to handle the form. Start by creating a simple
controller for displaying the registration form:
Next, you need a controller to handle the form. Start by creating a
controller that will display the registration form:

.. code-block:: php

// src/Acme/AccountBundle/Controller/AccountController.php
namespace Acme\AccountBundle\Controller;
// src/Controller/AccountController.php
namespace App\Controller;

use Acme\AccountBundle\Form\Model\Registration;
use Acme\AccountBundle\Form\Type\RegistrationType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Form\Model\Registration;
use App\Form\Type\RegistrationType;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class AccountController extends Controller
class AccountController extends AbstractController
{
public function registerAction()
{
$form = $this->createForm(RegistrationType::class, new Registration());

return $this->render('AcmeAccountBundle:Account:register.html.twig', [
return $this->render('Account/register.html.twig', [
'form' => $form->createView()
]);
}
Expand All @@ -237,18 +237,20 @@ and its template:

.. code-block:: html+jinja

{# src/Acme/AccountBundle/Resources/views/Account/register.html.twig #}
{# templates/Account/register.html.twig #}

{{ form_start(form, {'action': path('create'), 'method': 'POST'}) }}
{{ form_widget(form) }}

<input type="submit" />
{{ form_end(form) }}

Finally, create the controller which handles the form submission. This performs
the validation and saves the data into MongoDB:
Finally, create another action in ``AccountController``, which will handle
the form submission - perform its validation and save the User into MongoDB:

.. code-block:: php

// src/Controller/AccountController.php
public function createAction(DocumentManager $dm, Request $request)
{
$form = $this->createForm(RegistrationType::class, new Registration());
Expand All @@ -264,13 +266,13 @@ the validation and saves the data into MongoDB:
return $this->redirect(...);
}

return $this->render('AcmeAccountBundle:Account:register.html.twig', [
return $this->render('Account/register.html.twig', [
'form' => $form->createView()
]);
}

That's it! Your form now validates, and allows you to save the ``User``
object to MongoDB.
That's it! Your form now validates sent data and allows you to save
the ``User`` object to MongoDB.

.. _`UserInterface`: http://symfony.com/doc/current/book/security.html#book-security-user-entity
.. _`file`: http://symfony.com/doc/current/book/forms.html
Loading