symfony - Doctrine Extension Slug : avoid duplication -


i'm using doctrine , sf2 , have question slug extension : there way generate before flush ?

let have 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 , setters ... 

if 2 differents slugs : test , test_1

$brand=new brand(); $brand->setname('test'); $em->persist($brand); $brand2=new brand(); $brand2->setname('test'); $em->persist($brand2);     

the goal find target slug allready exist , have 1 db entry.

i can't use find() without generated slug, have idea ?

the main idea that, don't know how implement :

$brand=new brand(); $brand->setname('test'); $slug = $brand->getslug(); if( $obrand = $em->getrepository("demobundle:brand")->findonebyslug($slug)){     $brand = $obrand; } $em->persist($brand);    

thanks lot help.

you using right logic in solution think; there probalbly few glitches need take care of though :

$brand = new brand();  $brand->setname('test'); $slug = $brand->getslug();  // getslug() going work before persist ?  // if not, you'll have "simulate" generation of slug  // obtain string equivalent slug  $obrand = $em->getrepository("demobundle:brand")->findonebyslug($slug);  if(empty($obrand) { // empty, or is_null, depends on orm returns when finds nothing     $em->persist(brand); } else {     echo('slug ' . $brand->getslug() . ' in use !'); } 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -