Regenerating slug Doctrine2

2019-08-03 05:39发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-03 06:04

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.

查看更多
登录 后发表回答