在StofDoctrineExtensions中覆盖DoctrineExtensions方法 [英] Overriding DoctrineExtensions method in StofDoctrineExtensions

查看:136
本文介绍了在StofDoctrineExtensions中覆盖DoctrineExtensions方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改Gedmo \Tree \RepositoryUtils-> buildTree()方法的行为,因为我想更改返回数组的构造方式。

I am trying to change the behaviour of the Gedmo\Tree\RepositoryUtils->buildTree() method because I'd like to change the way the returned array is constructed.

我想跟随:

我有一个班级:

<?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);
    }

}

?>

现在我正在尝试使用此类而不是默认情况下调用时使用的类

Now I am trying to use this class instead of the one that is used by default when calling

$pagerepo = $this->getDoctrine()->getRepository('MyCorpCMSBundle:Page');

通过执行以下类型转换:

By doing the following typecasting:

$pagerepo = (jsandjqTreeCompatibleRepositoryUtils) $this->getDoctrine()->getRepository('MyCorpCMSBundle:Page');

但由于这不是java,这是不可能的。

But as this is no java, this is not possible.

我做错了什么?

推荐答案

可能有点儿迟到了,但我需要做同样的事情 - 这是其他人需要它的解决方案:

May be a little late, but i needed to to the same thing - here is the solution in case someone else needs it:

您的存储库类:

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方法:

in the MyRepositoryUtils you can overwrite the buildTree method:

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())
    {
    }
}

这篇关于在StofDoctrineExtensions中覆盖DoctrineExtensions方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆