Regenerating slug Doctrine2

拈花ヽ惹草 提交于 2019-12-13 00:35:33

问题


I'm trying to regerate slug for entities that I've created prior to install Sluggable extension for Doctrine2 as it's explained here: Regenerating slug, but when I set it to empty string or null, does not regerate it, it simply sets as null.

What I'm doing wrong?

The entity:

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;

/**
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product
{

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=200)
 * @Assert\NotBlank()
 */
protected $name;

/**
 * @ORM\Column(type="text")
 * @Assert\NotBlank()
 */
protected $description;

...

/**
 * @Gedmo\Slug(fields={"name"})
 * @ORM\Column(length=200, unique=true)
 */
private $slug;

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

}
}

And then I try this:

$repository = $this->getDoctrine()->getRepository('CommonBundle:Product');
$product = $repository->findOneBy(array("id"=>1));
$product->setSlug('');
$em = $this->getDoctrine()->getEntityManager();
$em->persist($product);
$em->flush();

回答1:


OK. I know what is happening. You are probably using an older version of the Doctrine Extensions, probably version 2.1.0. That would be the case if you are developing with Symfony 2.0.X as that is the version which was recommended. Check your deps file, you probably have something like:

[gedmo-doctrine-extensions]
    git=http://github.com/l3pp4rd/DoctrineExtensions.git
    version=v2.1.0

Those older versions of the extensions don´t support the behaviour described in the documentation (regenerate slug if it is empty). You would need to upgrade your version of the extensions, but this may mean that you may have to upgrade your Doctrine versions to 2.2, not really sure.



来源:https://stackoverflow.com/questions/11791738/regenerating-slug-doctrine2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!