I'm using doctrine and sf2 and i have a question about the slug extension : Is there any way to generate it before the flush ?
Let say i have a Brand Entity:
/**
* Brand
*
* @ORM\Table(indexes={@ORM\Index(name="name_idx", columns={"name"})})
* @ORM\Entity(repositoryClass="Shoesalley\Bundle\CoreBundle\Entity\BrandRepository")
*/
class Brand
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(length=128, unique=true)
*/
private $slug;
}
// getters and setters ...
if i do this i get 2 differents slugs : test and test_1
$brand=new Brand();
$brand->setName('test');
$em->persist($brand);
$brand2=new Brand();
$brand2->setName('Test');
$em->persist($brand2);
The goal would be to find that the target slug allready exist and only have 1 DB Entry.
I can't use find() without a generated slug, so does anyone have an idea ?
The main idea is something like that, but i don't know how to implement it :
$brand=new Brand();
$brand->setName('test');
$slug = $brand->getSlug();
if( $oBrand = $em->getRepository("DemoBundle:Brand")->findOneBySlug($slug)){
$brand = $oBrand;
}
$em->persist($brand);
Thanks a lot for your help.