0

Symfony 2 の Gedmo Tree Doctrine 拡張機能を使用して階層化されたエンティティがあります。Category エンティティのコードは次のとおりです。

<?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ます。これは期待どおりに機能します。

ただし、スラッグ フィールドをフォームに追加すると、エンティティを編集すると、最初にbands/rockフォーム フィールドに追加されます。これを変更しbands/rock-and-rollてフォームを送信すると、スラッグが更新され、期待どおりにはbands/bands-rock-and-rollなりませんbands/rock-and-roll

カテゴリを編集してスラッグ フィールドを に設定しrock-and-roll、フォームを送信すると、スラッグが に更新されbands/rock-and-rollます。スラッグはrock-and-roll更新後になると思います。

この動作を修正するにはどうすればよいですか? 基本的に、スラッグが提供されていない場合はハンドラーで自動生成されますが、提供されている場合は提供したものに正確に設定されます。

ありがとう

4

1 に答える 1

0

Gedmo Tree Doctrine 拡張機能のドキュメントを参照してください。これは相対的なコードです。スラッグは " TreeSlugHandler " メソッド = parentFieldName/field-You-Have-Inserted (これは、ナメクジ)。

特定のカテゴリのスラッグとカテゴリ ツリーに続く別のスラッグが同時に必要な場合は、別のプロパティ (例: cat_slug) を簡単な注釈で追加できます: @Gedmo\Slug(fields={"fieldYouWantToSlug"}) .

(「TreeSlugHandler」メソッドを使用して) スラッグを編集する (前例を変更する) たびに、すべてのサブカテゴリが新しいスラッグで更新されることに注意してください。

お役に立てば幸いです

于 2014-06-01T08:37:48.343 に答える