我正在尝试更改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,这是不可能的。
我做错了什么?
答案 0 :(得分:0)
可能有点晚了,但我需要做同样的事情 - 这是解决方案以防其他人需要它:
您的存储库类:
namespace Acme\Model\Repository;
use Doctrine\ORM\EntityManager;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use MyNamespace\GenericBundle\Repository\RepositoryUtils as MyRepositoryUtils;
/**
* Group Repository
*/
class CategoryRepository extends NestedTreeRepository
{
/**
* @param EntityManager $em
* @param ClassMetadata $class
*/
public function __construct(EntityManager $em, ClassMetadata $class)
{
parent::__construct($em, $class);
$this->repoUtils = new MyRepositoryUtils($this->_em, $this->getClassMetadata(), $this->listener, $this);
}
}
在MyRepositoryUtils中,你可以覆盖buildTree方法:
namespace MyNamespace\GenericBundle\Repository;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Tree\RepositoryUtils as GedmoRepositoryUtils;
class RepositoryUtils extends GedmoRepositoryUtils
{
/**
* {@inheritDoc}
*/
public function buildTree(array $nodes, array $options = array())
{
}
}