2012-07-27 1 views
0

페이지라는 엔티티에 대해 고유 한 URL 슬러그를 만들려고합니다. 민달팽이는 사이트와 관련된 페이지에서만 고유해야합니다.Doctrine2의 고유 한 슬러그

페이지 {site1.com/about, site2.com/about}의 URL 슬러그는 페이지를 식별 할 수있을만큼 고유합니다. 모든 사이트에서 고유 한 페이지는 매력적이지 않습니다. site3.com/about-21은 이상합니다.

나는 DoctrineExtensions을 슬러그 가능하게 만들려고 시도했지만 제안은 to write 사용자 정의가 있습니다. 아래는 나의 첫번째 시도이다. 엔티티에서 EntityManager를 가져올 수 없기 때문에 다른 슬러그가 고유한지 확인하는 데 문제가 있습니다.

제안 사항?

참조
/** 
* Slugify Title. To "re-slugify", set titleSlug to empty 
* @ORM\PrePersist 
*/ 
public function slugifyTitle($override = false) { 
    // We only slugify is titleSlug is empty 
    if (empty($this->titleSlug) or $override) { 

    if (!empty($this->title)) { 
     // Title not empty? slugify the title. SluggableListener line 220 
     $slug = Urlizer::transliterate($this->title); 
    } else { 
     // Title empty? make a slug based on the classname 
     $slug = get_class($this); 
    } 
    $this->titleSlug = Urlizer::urlize($slug); 
    $this->titleSlug = $this->uniqueify($this->titleSlug); 
    echo $this->titleSlug; 
    }; 
} 

/** 
* Make the string unique in the domain of: This site, this pagetype 
*/ 
public function uniqueify($someSlug) { 
    //return $someSlug . rand(); 
    // FIXME :-) 
    $em = $this->get('doctrine')->getEntityManager(); 
    $pageType = get_class($this); 
    $query = $em->createQuery("SELECT slug FROM Powma\ServiceBundle\Entity\$pageType pt"); 
    $slugs = $query->getResult(); 
    // find a unique one. 
} 

답변