symfony2 中具有多个实体的独特 slug

问题描述:

我有一个小的 symfony2 应用程序,用户可以在其中创建页面.每个页面都应该可以通过路由/{user_slug}/{page_slug} 访问.我有实体 user 和 page,我对这两个实体都使用了 sluggable 行为.为了找到正确的页面,user_slug 和 page_slug 的组合必须是唯一的.

I have a small symfony2 application where a user can create pages. Each page should be accessible via the route /{user_slug}/{page_slug}. I have the entities user and page and I use the sluggable behavior for both entities. In order to find the correct page the combination of user_slug and page_slug has to be unique.

检查 user_slug 和 page_slug 的组合是否唯一的最佳方法是什么?

What is the best way to check that the combination of user_slug and page_slug is uniqe?

试试这个:

public function findByUsernameAndSlug($username, $slug)
{
    $em = $this->getEntityManager();
    $query = $em->createQuery("
        SELECT g
        FROM Acme\PagesBundle\Entity\Page p
        JOIN p.owner u
        WHERE u.username = :username
        AND p.slug = :slug
    ")
            ->setParameter('username', $username)
            ->setParameter('slug', $slug);

    foreach ($query->getResult() as $goal) {
        return $goal;
    }

    return null;
}