具有多重关系的Slug



我有三个实体,比如ABCABC具有两个ManyToOne关系。我想为A生成一个段塞,由字段A.aB.bC.c组成。目前,我可以用A.aB.b生成一个段塞,代码如下:

A类:

<?php
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
/**
 * @ORMEntity(
 *     repositoryClass="AppBundleRepositoryARepository"
 * )
 */
class A
{
    /**
     * @var integer
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue
     */
    private $id;
    /**
     * @var string
     * @ORMColumn(type="string")
     */
    private $name;
    /**
     * @var B
     * @ORMManyToOne(targetEntity="B")
     */
    private $b;
    /**
     * @var C
     * @ORMManyToOne(targetEntity="C")
     */
    private $c;
    /**
     * @var string
     * @ORMColumn(type="string", unique=true)
     * @GedmoSlug(handlers={
     *      @GedmoSlugHandler(class="GedmoSluggableHandlerRelativeSlugHandler", options={
     *          @GedmoSlugHandlerOption(name="relationField", value="name"),
     *          @GedmoSlugHandlerOption(name="relationSlugField", value="alias"),
     *          @GedmoSlugHandlerOption(name="separator", value="-")
     *      })
     * }, separator="-", updatable=true, fields={"name"})
     */
    private $slug;
    // ...
}

B类:

<?php
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
/**
 * @ORMEntity(
 *     repositoryClass="AppBundleRepositoryBRepository"
 * )
 */
class B
{
    /**
     * @var integer
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue
     */
    private $id;
    /**
     * @var string
     * @ORMColumn(type="string")
     */
    private $name;
    /**
     * @var string
     * @ORMColumn(type="string")
     * @GedmoSlug(handlers={
     *      @GedmoSlugHandler(class="GedmoSluggableHandlerInversedRelativeSlugHandler", options={
     *          @GedmoSlugHandlerOption(name="relationClass", value="AppBundleEntityA"),
     *          @GedmoSlugHandlerOption(name="mappedBy", value="b"),
     *          @GedmoSlugHandlerOption(name="inverseSlugField", value="slug")
     *      })
     * }, fields={"name"})
     */
    private $alias;
    //...
 }

使用此代码,段塞A.slug类似于{{B.alias}}-{{A.name}}。我希望它是{{C.alias}}-{{B.alias}}-{{A.name}}。我尝试在GedmoSlughandlers数组选项中添加另一个SlugHander,但它忽略了第二个。我怎样才能做到这一点?

我已经用这个类解决了问题:

<?php
namespace AppHandler;
use GedmoSluggableMappingEventSluggableAdapter;
use GedmoToolWrapperAbstractWrapper;
class SecondaryRelativeGedmoSlugHandler extends GedmoSluggableHandlerRelativeSlugHandler
{
    /**
     * Used options
     *
     * @var array
     */
    private $usedOptions;
    /**
     * Callable of original transliterator
     * which is used by sluggable
     *
     * @var callable
     */
    private $originalTransliterator;
    /**
     * {@inheritDoc}
     */
    public function onChangeDecision(SluggableAdapter $ea, array &$config, $object, &$slug, &$needToChangeSlug)
    {
        $this->om = $ea->getObjectManager();
        $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object);
        $this->usedOptions = $config['handlers'][get_called_class()];
        if (!isset($this->usedOptions['separator'])) {
            $this->usedOptions['separator'] = self::SEPARATOR;
        }
        if (!$isInsert && !$needToChangeSlug) {
            $changeSet = $ea->getObjectChangeSet($this->om->getUnitOfWork(), $object);
            if (isset($changeSet[$this->usedOptions['relationField']])) {
                $needToChangeSlug = true;
            }
        }
    }
    /**
     * {@inheritDoc}
     */
    public function postSlugBuild(SluggableAdapter $ea, array &$config, $object, &$slug)
    {
        $this->originalTransliterator = $this->sluggable->getTransliterator();
        $this->sluggable->setTransliterator(array($this, 'transliterate'));
    }
    /**
     * Transliterates the slug and prefixes the slug
     * by relative one
     *
     * @param string $text
     * @param string $separator
     * @param object $object
     *
     * @return string
     */
    public function transliterate($text, $separator, $object)
    {
        // MIO
        $result = call_user_func_array(
            $this->originalTransliterator,
            array($text, $separator, $object)
        );
        $tokens = explode('/', $result);
        if(count($tokens) > 1) {
            $result = $tokens[count($tokens) - 1];
        }
        $wrapped = AbstractWrapper::wrap($object, $this->om);
        $relation = $wrapped->getPropertyValue($this->usedOptions['relationField']);
        if ($relation) {
            $wrappedRelation = AbstractWrapper::wrap($relation, $this->om);
            $slug = $wrappedRelation->getPropertyValue($this->usedOptions['relationSlugField']);
            if (isset($this->usedOptions['urilize']) && $this->usedOptions['urilize']) {
                $slug = call_user_func_array(
                    $this->originalTransliterator,
                    array($slug, $separator, $object)
                );
            }
            $result = $slug.$this->usedOptions['separator'].$result;
        }
        return $result;
    }
}

并使用这个注解

/**
 * @GedmoSlug(handlers={
 *      @GedmoSlugHandler(class="GedmoSluggableHandlerRelativeSlugHandler", value="shop", options={
 *          @GedmoSlugHandlerOption(name="relationField", value="shop"),
 *          @GedmoSlugHandlerOption(name="relationSlugField", value="name"),
 *          @GedmoSlugHandlerOption(name="separator", value="/"),
 *          @GedmoSlugHandlerOption(name="urilize", value=true)
 *      }),
 *     @GedmoSlugHandler(class="AppHandlerSecondaryRelativeGedmoSlugHandler", value="category", options={
 *          @GedmoSlugHandlerOption(name="relationField", value="category"),
 *          @GedmoSlugHandlerOption(name="relationSlugField", value="name"),
 *          @GedmoSlugHandlerOption(name="separator", value="/"),
 *          @GedmoSlugHandlerOption(name="urilize", value=true)
 *      })
 * },fields={"name"}, unique=true, updatable=true)
 * @ORMColumn(type="string", length=255, unique=true)
 */
private $slug;

相关内容

  • 没有找到相关文章

最新更新