0

Currently I cant get subclasses to appear in a list view using sonta admin bundle for symfony 2

I can get it working for create forms as per the advanced config page (http://sonata-project.org/bundles/admin/2-1/doc/reference/advance.html) but how can you do this with the list view?

If i pass the subclass in the url - list?subclass=MySubClassName and set the object in my listAction

$object = $this->admin->getNewInstance();
$this->admin->setSubject($object);

I can get the subject and configure the correct fields with configureListFields()

if ($subject instanceof MySubClassName) {
      $listMapper->add('MySubClassNameID');
      $listMapper->add('MySubClassNameKey');
      $listMapper->add('MySubClassNameStatus','text');
    }

but the end results table is always blank and the symfony debug toolbar seems to show that the db queries are looking for the parent class. Anyone got this to work?

3 Answers3

0

I'm not sure what you mean with those "subclasses" in the list view, but if you want to add a field form another entity (connected through a foreign key with yours) you can do it lie this:

$listMapper
    ->addIdentifier('id')
    ->addIdentifier('title')
    ->add('name')
    ->add('entity1.customField1')
    ->add('entity2.customField2');
Cristian Bujoreanu
  • 1,147
  • 10
  • 21
  • By subclasses i mean classes that inherit from a base class my admin handles (see 16.3 in the link i posted in the original question). so in the service definition they are defined `sonata.block.service.bulkjob: class: IDest\ContentBundle\Block\JobBulkBlockService arguments: [ "sonata.block.service.bulkjob", @templating,@sonata.admin.pool ] tags: - { name: sonata.block } calls: - [ setSubClasses, [ { AtlassianJob: IDest\ContentBundle\Document\Job\AtlassianJob , AwsJob: IDest\ContentBundle\Document\Job\AwsJob } ] ]` – Tom Whiston Dec 12 '14 at 10:29
  • sorry that mangled the code and i cant get it to diplay nicely, hopefully you get the idea – Tom Whiston Dec 12 '14 at 10:31
0

Incase anyone else faces this I found out how to do this.

To make it work in a way similar to the edit page you would pass the subclass in the url

...list?subclass=MySubClass

set the subject of your listAction in your custom admin crud controller

  public function listAction()
  {
    if (false === $this->admin->isGranted('LIST')) {
      throw new AccessDeniedException();
    }

    if ($listMode = $this->getRequest()->get('_list_mode')) {
      $this->admin->setListMode($listMode);
    }

    $this->admin->setSubject($this->admin->getNewInstance());

    $datagrid = $this->admin->getDatagrid();
    $formView = $datagrid->getForm()->createView();

    // set the theme for the current Admin Form
    $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());

    return $this->render($this->admin->getTemplate('list'), array(
      'action'     => 'list',
      'form'       => $formView,
      'datagrid'   => $datagrid,
      'csrf_token' => $this->getCsrfToken('sonata.batch'),
    ));
  }

and then over-ride the createQuery method in your Admin class

public function createQuery($context = 'list')
  {
    $cName = get_class($this->getSubject());
    $query = $this->getModelManager()->createQuery($cName);

    foreach ($this->extensions as $extension) {
      $extension->configureQuery($this, $query, $context);
    }

    return $query;
  }
0

If you pass anything with url parameters you should also override getPersistentParameters to add your url request to Pager, FilterForm and the form for batchActions (or others that appear on the list view)

<?php
class YourAdmin extends Admin
{
    public function getPersistentParameters()
    {
        if (!$this->getRequest()) {
            return array();
        }

        return array(
            'subclass' => $this->getRequest()->get('subclass'),
        );
    }
}
Grzegorz Krauze
  • 1,130
  • 12
  • 26