2014-04-16 2 views
0

doctrine과 sf2를 사용하고 있습니다. 슬러그 확장에 대한 질문이 있습니다. 플러시 전에 생성 할 방법이 있습니까? 그 대상 슬러그 allready을 찾기 위해 테스트 및 test_1Doctrine Extension Slug : 중복을 피하십시오.

$brand=new Brand(); 
$brand->setName('test'); 
$em->persist($brand); 
$brand2=new Brand(); 
$brand2->setName('Test'); 
$em->persist($brand2);  

이 목표는 다음과 같습니다 내가 이렇게하면 내가 2 differents의 슬러그를 얻을 수

/** 
* 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 ... 

:

하자 내가 브랜드 엔티티가 있다고 1 DB 항목 만 있습니다.

생성 된 슬러그 없이는 find()를 사용할 수 없으므로 누구나 아이디어를 얻을 수 있습니까?

주요 아이디어는 같은 것입니다,하지만 난 그것을 구현하는 방법을 모른다 :

$brand=new Brand(); 
$brand->setName('test'); 
$slug = $brand->getSlug(); 
if($oBrand = $em->getRepository("DemoBundle:Brand")->findOneBySlug($slug)){ 
    $brand = $oBrand; 
} 
$em->persist($brand); 

도와 주셔서 감사합니다 많이.

답변

-1

솔루션에서 올바른 논리를 사용하고 있다고 생각합니다. 당신이 신경 써야 할 몇 가지 문제가있을 수 있습니다.

$brand = new Brand(); 

$brand->setName('test'); 
$slug = $brand->getSlug(); 
// Is getSlug() going to work before persist ? 
// If not, you'll have to "simulate" the generation of a slug 
// to obtain a string equivalent to that slug 

$obrand = $em->getRepository("DemoBundle:Brand")->findOneBySlug($slug); 

if(empty($obrand) { // empty, or is_null, depends on what your orm returns when it finds nothing 
    $em->persist(brand); 
} else { 
    echo('Slug ' . $brand->getSlug() . ' is already in use !'); 
} 
+0

정확하게, 주요 문제는 여전히 슬러그 생성을 시뮬레이트하는 방법입니까? –

+0

Doctrine doc의 어딘가에 로직을 찾을 수 있어야합니다. 그래도 여전히 hackish가 될 것입니다. 예를 들어 [this] (https://github.com/guilhermeblanco/Doctrine2-Sluggable-Functional-Behavior/blob/master/lib/DoctrineExtensions/Sluggable/SlugGenerator.php) – np87