Skip to content

Commit

Permalink
add SonataAnnotationBundle to docs (#5041)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 authored and OskarStark committed Mar 28, 2018
1 parent 5e7e626 commit a611c22
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
111 changes: 111 additions & 0 deletions docs/cookbook/recipe_creating_an_admin_with_annotations.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
Creating an Admin with Annotations
==================================

If you are a fan of annotations, there is a 3rd party bundle that allows
using annotations instead of creating admin classes.

Download the SonataAnnotationBundle
-----------------------------------

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

.. code-block:: bash
$ composer require kunicmarko/sonata-annotation-bundle
How to use
----------

Let's say we have a ``BlogPost`` entity that is connected to a ``Category`` entity
like in the `getting started chapter`_:

.. code-block:: php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
// ...
/**
* @Sonata\Admin("BlogPost")
*
* @ORM\Table
* @ORM\Entity
*/
class BlogPost
{
// ...
/**
* @Sonata\ListField()
* @Sonata\FormField()
*
* @ORM\Column(name="title", type="string")
*/
private $title;
/**
* @Sonata\ListAssociationField(field="name")
* @Sonata\FormField()
*
* @ORM\ManyToOne(targetEntity="Category", inversedBy="blogPosts")
*/
private $category;
// ...
}
.. code-block:: php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
// ...
/**
* @Sonata\Admin("Category")
*
* @ORM\Table
* @ORM\Entity
*/
class Category
{
// ...
/**
* @Sonata\ListField()
* @Sonata\FormField()
*
* @ORM\Column(name="name", type="string")
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="BlogPost", mappedBy="category")
*/
private $blogPosts;
// ...
}
Do not forget to clear your cache:

.. code-block:: bash
$ bin/console cache:clear
You are done and you probably want to know how this looks like in the admin
interface. Well, let's find out by going to http://localhost:8000/admin

.. image:: ../images/admin_with_annotations.png
:align: center
:alt: Sonata Dashboard with SonataAnnotationBundle
:width: 700px

The rest of the Annotation and their options can be found `here`_.

.. _`getting started chapter`: https://sonata-project.org/bundles/admin/3-x/doc/getting_started/creating_an_admin.html
.. _`here`: https://github.com/kunicmarko20/SonataAnnotationBundle#annotations
Binary file added docs/images/admin_with_annotations.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ The demo website can be found at http://demo.sonata-project.org.
cookbook/recipe_data_mapper
cookbook/recipe_custom_view
cookbook/recipe_persisting_filters
cookbook/recipe_creating_an_admin_with_annotations

0 comments on commit a611c22

Please sign in to comment.