creating_an_admin.rst 6.4 KB

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