0

我找不到关于我在实体内部看到的“ORM 表达式”的好文档,所以我有点困惑。

我的用户实体中有这个:

/**
* @var ArrayCollection $administered
*
* @ORM\ManyToMany(targetEntity="Done\PunctisBundle\Entity\Brand", inversedBy="admins")
* @ORM\JoinTable(name="user_brand_administered")
**/
protected $administered; 

这工作正常,但我需要更简单的东西,我需要管理var来获取品牌实体的所有值,而不是像下面的代码那样加入表 user_brand_administrated。我怎样才能做到这一点?

4

1 回答 1

0

Why do you want all the value here ?

With a custom EntityRepository you can use a method to retrieve your object like you want.

For exemple.

class UserRepository extends EntityRepository
{
    public function findOneWithRelation($id)
    {
        $qb = $this->createQueryBuilder('u')
            ->select('u, b')
            ->leftJoin('u.administered, b')
            ->where('u.id = :id')
            ->setParameter('id', $id)
            ->getQuery()
            ->getResult();
    }
}

And attach this repository with your user entity:

/**
 * @Entity(repositoryClass = "Path\To\UserRepository")
 */
class User
{
// ...
}

Like this you can retrieve your user with all the brands.

Hope it's helpful.

Best regard.

于 2013-04-14T19:49:37.550 回答