Symfony 2 - 用Gedmo\Slug生成S ..

Joh*_*ann 6 symfony stofdoctrineextensions

我刚刚安装了doctrine扩展来使用Sluggable.

我这样做:

composer.json

"stof/doctrine-extensions-bundle": "1.2.*@dev"
Run Code Online (Sandbox Code Playgroud)

AppKernel.php

new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
Run Code Online (Sandbox Code Playgroud)

应用程序/配置/ config.yml

stof_doctrine_extensions:
    orm:
        default:
            sluggable: true
Run Code Online (Sandbox Code Playgroud)

Djoo\AppliBundle \实体\ Nomenclature.php

namespace Djoo\AppliBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\DBAL\Types\SmallIntType;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * Nomenclature
 *
 * 
 * @ORM\Table(name="app_nomenclature")
 * @ORM\Entity
 */
class Nomenclature
{
    .....
    /**
     * @var string
     *
     * @ORM\Column(name="titre", type="string", length=200, nullable=false)
     */
    private $titre;

    /**
     * @var string
     *
     * @ORM\Column(name="finess", type="string", length=10, nullable=true)
     */
    private $finess;

    /**
     * @Gedmo\Slug(fields={"titre","finess"},suffix=".html")
     * @ORM\Column(length=128, unique=true,nullable=true)
     */
    private $slug;

    public function getSlug() {
        return $this->slug;
    }

    public function setSlug($slug){
        $this->slug = $slug;
        return $this;
    }

}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我使这个为我的数据表中的旧值生成slug:

$filterBuilder = $this->get('doctrine.orm.entity_manager')>getRepository('DjooAppliBundle:Nomenclature')->createQueryBuilder('e')->orderBy('e.titre', 'asc');
$query = $filterBuilder->getQuery();
$nomenclatures = $query->getResult();

foreach($nomenclatures as $nomenclaturee){
    $nomenclature->setSlug(null);
    $this->get('doctrine.orm.entity_manager')->persist($nomenclature);
    $this->get('doctrine.orm.entity_manager')->flush();
 }
Run Code Online (Sandbox Code Playgroud)

我没有错误,但我的旧值是一个空的slu .. 我尝试创建一个新元素,我有一个很好的slug.你有想法吗?

谢谢

lso*_*uza 7

要更改slug,您必须更改相关属性.您可以在末尾添加空格$titre,保存,更改它并再次保存.这将冲洗slu ..