我正在尝试更改 Gedmo\Tree\RepositoryUtils->buildTree() 方法的行为,因为我想更改返回数组的构造方式。
我正在尝试关注:
我有一堂课:
<?php
namespace MyCorp\CMSBundle\Util;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Gedmo\Exception\InvalidArgumentException;
/**
* Description of jsandjqTreeCompatibleRepositoryUtils
*
* @author peterrus
*/
class jsandjqTreeCompatibleRepositoryUtils extends Gedmo\Tree\RepositoryUtils {
public function buildTree(array $nodes, array $options = array()) {
$meta = $this->getClassMetadata();
$nestedTree = $this->repo->buildTreeArray($nodes);
$default = array(
'decorate' => false,
'rootOpen' => '<ul>',
'rootClose' => '</ul>',
'childOpen' => '<li>',
'childClose' => '</li>',
'nodeDecorator' => function ($node) use ($meta) {
// override and change it, guessing which field to use
if ($meta->hasField('title')) {
$field = 'title';
} elseif ($meta->hasField('name')) {
$field = 'name';
} else {
throw new InvalidArgumentException("Cannot find any representation field");
}
return $node[$field];
}
);
$options = array_merge($default, $options);
// If you don't want any html output it will return the nested array
if (!$options['decorate']) {
return $nestedTree;
}
if (!count($nestedTree)) {
return '';
}
$build = function($tree) use (&$build, &$options) {
$output = is_string($options['rootOpen']) ? $options['rootOpen'] : $options['rootOpen']($tree);
foreach ($tree as $node) {
$output .= is_string($options['childOpen']) ? $options['childOpen'] : $options['childOpen']($node);
$output .= $options['nodeDecorator']($node);
if (count($node['children']) > 0) {
$output .= $build($node['children']);
}
$output .= $options['childClose'];
}
return $output . $options['rootClose'];
};
return $build($nestedTree);
}
}
?>
现在我正在尝试使用此类而不是调用时默认使用的类
$pagerepo = $this->getDoctrine()->getRepository('MyCorpCMSBundle:Page');
通过执行以下类型转换:
$pagerepo = (jsandjqTreeCompatibleRepositoryUtils) $this->getDoctrine()->getRepository('MyCorpCMSBundle:Page');
但是因为这不是java,所以这是不可能的。
我究竟做错了什么?