0

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

<?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。我希望蛞蝓rock-and-roll在更新之后。

我需要做什么来解决这种行为?如果没有提供,我本质上希望使用处理程序自动生成 slug,但如果我确实提供了它,则将其设置为我提供的内容。

谢谢

4

1 回答 1

0

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

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

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

我希望我有帮助

于 2014-06-01T08:37:48.343 回答