-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add SonataAnnotationBundle to docs (#5041)
- Loading branch information
1 parent
5e7e626
commit a611c22
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
docs/cookbook/recipe_creating_an_admin_with_annotations.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters