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 14
14
use Sonata \AdminBundle \Admin \AdminExtension ;
15
15
use Sonata \AdminBundle \Admin \AdminInterface ;
16
16
use Symfony \Cmf \Bundle \CoreBundle \Model \ChildInterface ;
17
+ use Doctrine \ODM \PHPCR \HierarchyInterface ;
17
18
18
19
/**
19
20
* Admin extension to handle child models.
@@ -29,14 +30,26 @@ class ChildExtension extends AdminExtension
29
30
*/
30
31
public function alterNewInstance (AdminInterface $ admin , $ object )
31
32
{
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 ;
34
37
}
35
38
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:
38
46
$ 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 )));
40
53
}
41
54
}
42
55
}
Original file line number Diff line number Diff line change 12
12
namespace Symfony \Cmf \Bundle \CoreBundle \Model ;
13
13
14
14
/**
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.
16
19
*/
17
20
interface ChildInterface
18
21
{
19
22
/**
20
23
* @param $parent object
21
24
*/
22
- public function setParentDocument ($ parent );
25
+ public function setParentObject ($ parent );
23
26
24
27
/**
25
28
* @return object
26
29
*/
27
- public function getParentDocument ();
30
+ public function getParentObject ();
28
31
}
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ public function testAlterNewInstance()
50
50
51
51
$ child = $ this ->getMock ('Symfony\Cmf\Bundle\CoreBundle\Model\ChildInterface ' );
52
52
$ child ->expects ($ this ->once ())
53
- ->method ('setParentDocument ' )
53
+ ->method ('setParentObject ' )
54
54
->with ($ this ->equalTo ($ parent ));
55
55
56
56
$ extension = new ChildExtension ();
You can’t perform that action at this time.
0 commit comments