使用Gedmo TreeSlugHandler编辑Slug

时间:2014-01-15 23:53:01

标签: forms symfony doctrine-orm doctrine-extensions

我有一个使用Symfony 2中的Gedmo Tree Doctrine扩展分层的实体.Categics实体的代码是:

<?php

namespace MD\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

use Doctrine\ORM\Mapping as ORM;

use Gedmo\Mapping\Annotation as Gedmo;

use MD\Entity\Extension\Treeable;

/**
 * Category
 *
 * @ORM\Entity(repositoryClass="MD\Entity\Repository\CategoryRepository")
 *
 * @Gedmo\Tree(type="nested")
 */
class Category
{
    /**
     * Entity Extensions
     */
    use Treeable;


    /**
     * The ID of the category
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * The title of the category
     *
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank(message="category.title.not_blank")
     * @Assert\Length(
     *      max=255,
     *      maxMessage="category.title.length.max"
     * )
     */
    protected $title;

    /**
     * The description of the category
     *
     * @var string
     *
     * @ORM\Column(type="text")
     *
     * @Assert\NotBlank(message="category.description.not_blank")
     */
    protected $description;

    /**
     * The parent of the category
     *
     * @var Category
     *
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
     *
     * @Gedmo\TreeParent
     */
    protected $parent;

    /**
     * The children of the category
     *
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"persist"})
     * @ORM\OrderBy({"left" = "ASC"})
     */
    protected $children;

    /**
     * The slug of the category
     *
     * @var string
     *
     * @ORM\Column(type="string", length=255, unique=true)
     *
     * @Gedmo\Slug(handlers={
     *      @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\TreeSlugHandler", options={
     *          @Gedmo\SlugHandlerOption(name="parentRelationField", value="parent"),
     *          @Gedmo\SlugHandlerOption(name="separator", value="/")
     *      })
     * }, fields={"title"})
     */
    protected $slug;



    /**
     * Constructor
     */
    public function __construct()
    {
        $this->children = new ArrayCollection();
    }

    /**
     * Get the ID of the category
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the title of the category
     *
     * @param string $title
     *
     * @return $this
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get the title of the category
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set the description of the category
     *
     * @param string $description
     *
     * @return $this
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get the description of the category
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set the parent of the category
     *
     * @param Category $parent
     *
     * @return $this
     */
    public function setParent(Category $parent = null)
    {
        $this->parent = $parent;

        if (null !== $parent) {
            $parent->addChild($this);
        }

        return $this;
    }

    /**
     * Get the parent of the category
     *
     * @return Category
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Add a child to the category
     *
     * @param Category $child
     *
     * @return $this
     */
    public function addChild(Category $child = null)
    {
        if (!$this->children->contains($child)) {
            $this->children->add($child);
            $child->setParent($this);
        }

        return $this;
    }

    /**
     * Get the children of the category
     *
     * @return ArrayCollection
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * Set the slug of the category
     *
     * @param string $slug
     *
     * @return $this
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * Get the slug of the category
     *
     * @return string
     */
    public function getSlug()
    {
        return $this->slug;
    }



    /** Magic Methods */

    /**
     * Return a string representation of the category
     *
     * @return string
     */
    public function __toString()
    {
        return $this->getTitle();
    }
}

鉴于标题为Bands的类别和标题为Rock的子类别,后一类别在创建时具有bands/rock标题。这可以按预期工作。

当我将slug字段添加到表单时,当我编辑实体时,我最初将bands/rock添加到表单字段中。如果我将其更改为bands/rock-and-roll并提交表单,那么slug会更新为bands/bands-rock-and-roll而不是bands/rock-and-roll,如我所料。

如果我编辑类别并将slug字段设置为rock-and-roll,则提交表单,slug会更新为bands/rock-and-roll。我希望更新后的slug为rock-and-roll

我需要做些什么才能解决此问题?我基本上想要使用处理程序自动生成slug,如果它没有提供,但是如果我提供它就要设置为我提供的。

由于

1 个答案:

答案 0 :(得分:0)

查看Gedmo Tree Doctrine extension的文档及其相关代码,这不是一个错误的行为,因为slug是相应的“ TreeSlugHandler ”方法= parentFieldName / field -You-Have-Inserted (每次编辑slug时都会发生这种情况)。

如果您需要同一时刻的特定类别和另一个类别树的slug,您可以使用简单注释添加另一个属性(例如:cat_slug):@Gedmo \ Slug(fields = {“fieldYouWantToSlug “})。

请记住,每次(使用“TreeSlugHandler”方法)编辑slug(更改先例)时,每个子类别都将使用新的slug进行更新。

我希望我有所帮助