creating_an_admin.rst 6.4 KB

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