Skip to content

Commit

Permalink
Add tests and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelVella committed Apr 12, 2013
1 parent 9d0d757 commit 72c11bc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Resources/doc/reference/architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,48 @@ Note that you can use both the Bundle:Controller format or a `service name`_ to
specify what controller to load. If you provide null instead of SonataNewsBundle:PostAdmin,
you will not need to create a controller class and the system will use the default.

Create child admins
---------------------------

Let's say you have a PostAdmin and a CommentAdmin. You can optionally declare the CommentAdmin
to be a child of the PostAdmin. This will create new routes like, for example, ``/post/{id}/comment/list``,
where the comments will automatically be filtered by post.

To do this, you first need to call the ``addChild`` method in your PostAdmin service configuration :

.. code-block:: xml
<!-- app/config/config.xml -->
<service id="sonata.news.admin.post" class="Sonata\NewsBundle\Admin\CommentAdmin">
...
<call method="addChild">
<argument type="service" id="sonata.news.admin.comment" />
</call>
</service>
Then, you have to set the CommentAdmin ``parentAssociationMapping`` attribute to ``post`` :

.. code-block:: php
<?php
namespace Sonata\NewsBundle\Admin;
...
class CommentAdmin extends Admin
{
protected $parentAssociationMapping = 'post';
// OR
public function getParentAssociationMapping()
{
return 'post';
}
}
It also possible to set a dot-separated value, like ``post.author``, if your parent and child admins are not directly related.

.. _`Django Project Website`: http://www.djangoproject.com/
.. _`service name`: http://symfony.com/doc/2.1/cookbook/controller/service.html
35 changes: 35 additions & 0 deletions Tests/Admin/BaseAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function getClassMetaData()

class CommentAdmin extends Admin
{
public function setParentAssociationMapping($associationMapping)
{
$this->parentAssociationMapping = $associationMapping;
}

public function setClassnameLabel($label)
{
$this->classnameLabel = $label;
Expand Down Expand Up @@ -221,4 +226,34 @@ public function testToString()

$this->assertEquals("", $admin->toString(false));
}

public function testGetFilterParameters()
{
$authorId = uniqid();

$postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');

$commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
$commentAdmin->setParentAssociationMapping('post.author');
$commentAdmin->setParent($postAdmin);

$request = $this->getMock('Symfony\Component\HttpFoundation\Request', array('get'));
$request->expects($this->any())
->method('get')
->will($this->returnValue($authorId));

$commentAdmin->setRequest($request);

$modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
$modelManager->expects($this->any())
->method('getDefaultSortValues')
->will($this->returnValue(array()));

$commentAdmin->setModelManager($modelManager);

$parameters = $commentAdmin->getFilterParameters();

$this->assertTrue(isset($parameters['post__author']));
$this->assertEquals(array('value' => $authorId), $parameters['post__author']);
}
}

0 comments on commit 72c11bc

Please sign in to comment.