File tree Expand file tree Collapse file tree 3 files changed +25
-9
lines changed
Tests/Unit/Admin/Extension Expand file tree Collapse file tree 3 files changed +25
-9
lines changed Original file line number Diff line number Diff line change 1414use Sonata \AdminBundle \Admin \AdminExtension ;
1515use Sonata \AdminBundle \Admin \AdminInterface ;
1616use Symfony \Cmf \Bundle \CoreBundle \Model \ChildInterface ;
17+ use Doctrine \ODM \PHPCR \HierarchyInterface ;
1718
1819/**
1920 * Admin extension to handle child models.
@@ -29,14 +30,26 @@ class ChildExtension extends AdminExtension
2930 */
3031 public function alterNewInstance (AdminInterface $ admin , $ object )
3132 {
32- if (!$ object instanceof ChildInterface) {
33- throw new \InvalidArgumentException ('Expected ChildInterface, got ' . get_class ($ object ));
33+ if (!$ admin ->hasRequest ()
34+ || !$ parentId = $ admin ->getRequest ()->get ('parent ' )
35+ ) {
36+ return ;
3437 }
3538
36- if ($ admin ->hasRequest () && $ parentId = $ admin ->getRequest ()->get ('parent ' )) {
37- if ($ parent = $ admin ->getModelManager ()->find (null , $ parentId )) {
39+ $ parent = $ admin ->getModelManager ()->find (null , $ parentId );
40+ if (!$ parent ) {
41+ return ;
42+ }
43+
44+ switch ($ object ) {
45+ case $ object instanceof HierarchyInterface:
3846 $ object ->setParentDocument ($ parent );
39- }
47+ break ;
48+ case $ object instanceof ChildInterface:
49+ $ object ->setParentObject ($ parent );
50+ break ;
51+ default :
52+ throw new \InvalidArgumentException (sprintf ('Class %s is not supported ' , get_class ($ object )));
4053 }
4154 }
4255}
Original file line number Diff line number Diff line change 1212namespace Symfony \Cmf \Bundle \CoreBundle \Model ;
1313
1414/**
15- * An interface for models with a parent document.
15+ * An interface for models with a parent object.
16+ *
17+ * Note that PHPCR-ODM documents will most likely use the HierarchyInterface
18+ * of PHPCR-ODM instead.
1619 */
1720interface ChildInterface
1821{
1922 /**
2023 * @param $parent object
2124 */
22- public function setParentDocument ($ parent );
25+ public function setParentObject ($ parent );
2326
2427 /**
2528 * @return object
2629 */
27- public function getParentDocument ();
30+ public function getParentObject ();
2831}
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ public function testAlterNewInstance()
5050
5151 $ child = $ this ->getMock ('Symfony\Cmf\Bundle\CoreBundle\Model\ChildInterface ' );
5252 $ child ->expects ($ this ->once ())
53- ->method ('setParentDocument ' )
53+ ->method ('setParentObject ' )
5454 ->with ($ this ->equalTo ($ parent ));
5555
5656 $ extension = new ChildExtension ();
You can’t perform that action at this time.
0 commit comments