I have been doing a lot of Symfony2 development lately for our project ProTalk and one of the things we needed was a backend for our database. This should be a fairly simple backend to start with, just an easy way to get data into the database. So I thought I would use the SonataAdminBundle to easy generate this backend based on the doctrine2 entities that we already have. Eventually, I got it working, but it took me some time to find out exactly how, so I thought I’d share my experiences.First of all, after trying some things by hand, I found this great blog post to help me get the bundle installed with login functionality as well. This helped me a lot since the actual documentation on the bundle was not clear to me. I found the overall documentation on this bundle bad. It assumes certain knowledge and terminology that someone with little experience in Symfony2 doesn’t have.
After getting the bundles correctly installed, I got on to the next step which was actually creating my first menu item in the backend. I started out with something simple, so I took a table called tag, which only contains an id and a name field, as an example. The documentation stated I should add some code to the config.yml file (which I didn’t have before but created during the installation). So I added the following code to the config.yml file:
services: protalk.common.admin.tag: class: Protalk\AdminBundle\Admin\TagAdmin tags: - { name: sonata.admin, manager_type: orm, group: protalk_common, label: Tag} arguments: [null, Protalk\MediaBundle\Entity\Tag, ProtalkAdminBundle:TagAdmin]
Make sure you also add the group (in this case protalk_common) to the section: sonata_admin / dashboard / groups in the config.yml file. This can be used to make menu’s and submenu’s. After this, I needed to create the admin class as shown in the code above, this is placed in the Protalk/AdminBundle/Admin directory. I created a special AdminBundle for the backend, but this is not necessary at all, you could also use an existing bundle as well. The code for my Admin class looks like this:
namespace Protalk\AdminBundle\Admin; use Sonata\AdminBundle\Admin\Admin; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Validator\ErrorElement; use Sonata\AdminBundle\Form\FormMapper; class TagAdmin extends Admin { protected function configureFormFields(FormMapper $formMapper) { $formMapper->add('name'); } protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper->add('name'); } protected function configureListFields(ListMapper $listMapper) { $listMapper->addIdentifier('name'); } public function validate(ErrorElement $errorElement, $object) { $errorElement->with('name')->assertMaxLength(array('limit' => 50))->end(); } }
Now, according to the documentation I found online, this should be it and it should work now. But they are wrong, because we also need a controller to be able to get it working. These controllers are in fact empty controllers, since the SonataAdminBundle will take care of all the actions for us. I created the TagController.php in the Protalk/AdminBundle/Controller directory and it contains this code:
namespace Protalk\AdminBundle\Controller; use Sonata\AdminBundle\Controller\CRUDController as Controller; class TagAdminController extends Controller { }
That is it really. For me, the documentation made no sense until I learned (thanks to my friend Stefan Koopmanschap) that I needed the controller as well. Finally it all works and I am ready to do more advanced stuff with the SonataAdminBundle.