creating_an_admin.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. Creating an Admin
  2. =================
  3. You've been able to get the admin interface working in :doc:`the previous
  4. chapter <installation>`. In this tutorial, you'll learn how to tell SonataAdmin
  5. how an admin can manage your models.
  6. Step 0: Create a Model
  7. ----------------------
  8. For the rest of the tutorial, you'll need some sort of model. In this tutorial,
  9. two very simple ``Post`` and ``Tag`` entities will be used. Generate them by
  10. using these commands:
  11. .. code-block:: bash
  12. $ php app/console doctrine:generate:entity --entity="AppBundle:Category" --fields="name:string(255)" --no-interaction
  13. $ php app/console doctrine:generate:entity --entity="AppBundle:BlogPost" --fields="title:string(255) body:textdraft:boolean" --no-interaction
  14. After this, you'll need to tweak the entities a bit:
  15. .. code-block:: php
  16. // src/AppBundle/Entity/BlogPost.php
  17. // ...
  18. class BlogPost
  19. {
  20. // ...
  21. /**
  22. * @ORM\ManyToOne(targetEntity="Category", inversedBy="blogPosts")
  23. */
  24. private $category;
  25. public function setCategory(Category $category)
  26. {
  27. $this->category = $category;
  28. }
  29. public function getCategory()
  30. {
  31. return $this->category;
  32. }
  33. // ...
  34. }
  35. .. code-block:: php
  36. // src/AppBundle/Entity/Category.php
  37. // ...
  38. use Doctrine\Common\Collections\ArrayCollection;
  39. // ...
  40. class Category
  41. {
  42. // ...
  43. /**
  44. * @ORM\OneToMany(targetEntity="BlogPost", mappedBy="category")
  45. */
  46. private $blogPosts;
  47. public function __construct()
  48. {
  49. $this->blogPosts = new ArrayCollection();
  50. }
  51. public function getBlogPosts()
  52. {
  53. return $this->blogPosts;
  54. }
  55. // ...
  56. }
  57. After this, create the schema for these entities:
  58. .. code-block:: bash
  59. $ php app/console doctrine:schema:create
  60. .. note::
  61. This article assumes you have basic knowledge of the Doctrine2 ORM and
  62. you've set up a database correctly.
  63. Step 1: Create an Admin Class
  64. -----------------------------
  65. SonataAdminBundle helps you manage your data using a graphical interface that
  66. will let you create, update or search your model instances. The bundle relies
  67. on Admin classes to know which models will be managed and how these actions
  68. will look like.
  69. An Admin class decides which fields to show on a listing, which fields are used
  70. to find entries and how the create form will look like. Each model will have
  71. its own Admin class.
  72. Knowing this, let's create an Admin class for the ``Category`` entity. The
  73. easiest way to do this is by extending ``Sonata\AdminBundle\Admin\Admin``.
  74. .. code-block:: php
  75. // src/AppBundle/Admin/CategoryAdmin.php
  76. namespace AppBundle\Admin;
  77. use Sonata\AdminBundle\Admin\Admin;
  78. use Sonata\AdminBundle\Datagrid\ListMapper;
  79. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  80. use Sonata\AdminBundle\Form\FormMapper;
  81. class CategoryAdmin extends Admin
  82. {
  83. protected function configureFormFields(FormMapper $formMapper)
  84. {
  85. $formMapper->add('name', 'text');
  86. }
  87. protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  88. {
  89. $datagridMapper->add('name');
  90. }
  91. protected function configureListFields(ListMapper $listMapper)
  92. {
  93. $listMapper->addIdentifier('name');
  94. }
  95. }
  96. So, what does this code do?
  97. * **Line 11-14**: These lines configure which fields are displayed on the edit
  98. and create actions. The ``FormMapper`` behaves similar to the ``FormBuilder``
  99. of the Symfony Form component;
  100. * **Line 16-19**: This method configures the filters, used to filter and sort
  101. the list of models;
  102. * **Line 21-24**: Here you specify which fields are shown when all models are
  103. listed (the ``addIdentifier()`` method means that this field will link to the
  104. show/edit page of this particular model).
  105. This is the most basic example of the Admin class. You can configure a lot more
  106. with the Admin class. This will be covered by other, more advanced, articles.
  107. Step 3: Register the Admin class
  108. --------------------------------
  109. You've now created an Admin class, but there is currently no way for the
  110. SonataAdminBundle to know that this Admin class exists. To tell the
  111. SonataAdminBundle of the existence of this Admin class, you have to create a
  112. service and tag it with the ``sonata.admin`` tag:
  113. .. code-block:: yaml
  114. # app/config/services.yml
  115. # ...
  116. services:
  117. admin.category:
  118. class: AppBundle\Admin\CategoryAdmin
  119. arguments: [~, AppBundle\Entity\Category, ~]
  120. tags:
  121. - { name: sonata.admin, manager_type: orm, label: Category }
  122. The constructor of the base Admin class has many arguments. SonataAdminBundle
  123. provides a compiler pass which takes care of configuring it correctly for you.
  124. You can often tweak things using tag attributes. The code shown here is the
  125. shortest code needed to get it working.
  126. Step 4: Register SonataAdmin custom Routes
  127. ------------------------------------------
  128. SonataAdminBundle generates routes for the Admin classes on the fly. To load these
  129. routes, you have to make sure the routing loader of the SonataAdminBundle is executed:
  130. .. code-block:: yaml
  131. # app/config/routing.yml
  132. # ...
  133. _sonata_admin:
  134. resource: .
  135. type: sonata_admin
  136. prefix: /admin
  137. View the Category Admin Interface
  138. ---------------------------------
  139. Now you've created the admin class for your category, you probably want to know
  140. how this looks like in the admin interface. Well, let's find out by going to
  141. http://localhost:8000/admin
  142. .. image:: ../images/getting_started_category_dashboard.png
  143. Feel free to play around and add some categories, like "Symfony" and "Sonata
  144. Project". In the next chapters, you'll create an admin for the ``BlogPost``
  145. entity and learn more about this class.
  146. .. tip::
  147. If you're not seeing the nice labels, but instead something like
  148. "link_add", you should make sure that you've `enabled the translator`_.
  149. .. _`enabled the translator`: http://symfony.com/doc/current/book/translation.html#configuration