我有一个使用Symfony 2中的Gedmo Tree Doctrine扩展分层的实体。类别实体的代码为:
<?php
namespace MDEntity;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
use MDEntityExtensionTreeable;
/**
* Category
*
* @ORMEntity(repositoryClass="MDEntityRepositoryCategoryRepository")
*
* @GedmoTree(type="nested")
*/
class Category
{
/**
* Entity Extensions
*/
use Treeable;
/**
* The ID of the category
*
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* The title of the category
*
* @var string
*
* @ORMColumn(type="string", length=255)
*
* @AssertNotBlank(message="category.title.not_blank")
* @AssertLength(
* max=255,
* maxMessage="category.title.length.max"
* )
*/
protected $title;
/**
* The description of the category
*
* @var string
*
* @ORMColumn(type="text")
*
* @AssertNotBlank(message="category.description.not_blank")
*/
protected $description;
/**
* The parent of the category
*
* @var Category
*
* @ORMManyToOne(targetEntity="Category", inversedBy="children")
* @ORMJoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*
* @GedmoTreeParent
*/
protected $parent;
/**
* The children of the category
*
* @var ArrayCollection
*
* @ORMOneToMany(targetEntity="Category", mappedBy="parent", cascade={"persist"})
* @ORMOrderBy({"left" = "ASC"})
*/
protected $children;
/**
* The slug of the category
*
* @var string
*
* @ORMColumn(type="string", length=255, unique=true)
*
* @GedmoSlug(handlers={
* @GedmoSlugHandler(class="GedmoSluggableHandlerTreeSlugHandler", options={
* @GedmoSlugHandlerOption(name="parentRelationField", value="parent"),
* @GedmoSlugHandlerOption(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,但如果我确实提供它,则将其设置为我提供的确切内容。
谢谢
查看 Gedmo Tree Doctrine 扩展的文档及其相对代码,这不是一个错误的行为,因为 slug 是用 "TreeSlugHandler" 方法 = parentFieldName/field-You-Have-Insert(每次编辑 slug 时都会发生这种情况)组成的。
如果你需要同时一个特定类别的 slug 和类别树后面的另一个 slug,你可以添加另一个属性(例如:cat_slug),使用简单的注释:@Gedmo\Slug(fields={"fieldYouWantToSlug"})。
请记住,每次(使用"TreeSlugHandler"方法)编辑 slug (更改先例)时,每个子类别都将使用新的 slug 进行更新。
我希望我有帮助