1

我正在尝试更改 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,所以这是不可能的。

我究竟做错了什么?

4

1 回答 1

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())
    {
    }
}
于 2014-08-22T16:00:02.717 回答