2014-10-17 35 views
6

我剛剛安裝了使用Sluggable的原則擴展。Symfony 2 - 用Gedmo Slug生成S 012

我有這樣的:

composer.json

"stof/doctrine-extensions-bundle": "1.2.*@dev" 

AppKernel.php

new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(), 

應用程序/配置/ config.yml

stof_doctrine_extensions: 
    orm: 
     default: 
      sluggable: true 

Djoo \ AppliBundle \ Entity \ 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; 
    } 

} 

在我的控制器我有這樣的生成舊價值觀塞在我的數據表:

$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(); 
} 

我沒有錯誤,但我的老值是空塞。我試圖創造一個新的元素,我有一個好的slu。。你有沒有想法?

謝謝

+1

我認爲slu is僅爲新持久對象生成。 – fejese 2014-10-17 13:42:54

回答

7

要更改slug您必須更改相關屬性。您可以在$titre的末尾添加一個空格,將其保存,然後將其更改並重新保存。這將沖洗slu。。

+1

Yeaaaa,thank you $ entity = $ em-> getRepository($ entityName) - > find($ id); \t \t $ titre = $ entity-> getTitre(); \t \t $ entity - > setSlug(null); \t \t $ entity - > setTitre($ titre。「」); \t \t $ em - > flush(); – Johann 2014-10-17 14:27:43

+1

既然v2.3.8 setSlug(null)會做的。 – 10us 2016-06-29 14:22:48

+0

它沒有爲我工作。我不得不手動重新生成slu according根據http://stackoverflow.com/a/18672588/1179841 – Sithu 2017-02-02 15:46:40

2
$uow = $em->getUnitOfWork();  
$uow->propertyChanged($entity, 'slug', NULL, NULL);  
$uow->scheduleForUpdate($entity);  
$em->flush(); 
+0

這適用於我和我想知道爲什麼它有-1 ????。我剛剛添加了+1。另外請記住,您可能想要將'ObjectManager'輸入到'EntityManager'中,這樣您就可以完成'getUnitOfWork()'的代碼完成:'/ ** @var ObjectManager | EntityManager $ em * /' – 2017-03-30 16:52:24

2

sluggable documentation狀態如下:

在情況下,如果你想根據sluggable 領域蛞蝓再生本身,設置蛞蝓爲null。

<?php 
$entity = $em->find('Entity\Something', $id); 
$entity->setSlug(null); 

$em->persist($entity); 
$em->flush(); 

它確實爲我工作。

+0

不起作用爲了我。字段標註如下所示:@ORM \ Column(name =「slug」,unique = true,nullable = true)''@Gedmo \ Slug(fields = {「name」,「city」},updatable = true)'' – 2017-03-30 16:54:00

2

爲什麼沒有爲OP工作,但對於其他人的工作(如@gregor):

當創建塞,你的第一反應是與此列配置創建蛞蝓屬性:

..  

@ORM\Column(unique=true, nullable=false) 
private $slug; 
.. 

當運行app/console doctrine:schema:update,這將導致2個SQL語句:

ALTER TABLE ... ADD slug ... NOT NULL 
CREATE UNIQUE INDEX...`. 

默認列slug將充滿了價值''(空字符串)會導致第二個語句失敗,並顯示(Duplicate entry '')錯誤。現在你有兩個選擇:

選項A:忽略第二個聲明

的故障如果您忽略該錯誤,後來又嘗試使用記錄的方法$entity->setSlug(null)就不會有什麼問題手動生成蛞蝓。它會工作,因爲通過使用$entity->setSlug(null)你會讓學說知道財產slug已被改變(從''null),這反過來會觸發內部$uow->propertyChanged()$uow->scheduleForUpdate()(感謝@塞巴斯蒂安拉杜爲他的例子)。 Sluggable擴展也會注意到這一更改,並會重新生成段塞。現在,由於所有的slu are都是獨一無二的,下次運行app/console doc:schema:update時,它將成功創建slug的索引,並且您的模式將完全同步。

選項B:修改slug場是nullable 注意到錯誤後,你的本能會標記slug場爲nullable,使索引創建成功:

..  

@ORM\Column(unique=true, nullable=true) 
private $slug; 
.. 

這將導致slug列有因爲它的默認值爲NULL。現在,當您嘗試使用記錄的$entity->setSlug(null)方法時,它將無法工作(就像OP發佈的那樣)。發生這種情況是因爲$entity->slug屬性已經是NULL。因此,當您使用$entity->setSlug(null)原則檢測不到更改時,因此Sluggable從不觸發再生行爲。爲了觸發的變化有兩個答案:

  • 黑客以增加空間的塞源屬性$entity -> setTitre($titre." ");(但是這會導致額外的空間,你將不得不後修剪)

  • 方法,通過@Sebastian拉杜,在那裏他展示瞭如何告訴學說直接在現場被改變(我個人比較喜歡這個,不知道爲什麼它是不公平的downvoted)

希望這有助於你瞭解一個好一點的內在工作原則和擴展。