【问题标题】:Symfony2 filter related entities in controllerSymfony2 过滤控制器中的相关实体
【发布时间】:2014-06-23 11:08:12
【问题描述】:

我有一个实体“Note”,它以多对多关系链接到我的实体“用户”。从我的用户实体中,我想获取所有符合特定条件的笔记。

我知道通过 ORM 你可以这样做:

$this->getDoctrine()->getRepository('PmbLicensingBundle:Note')->findBy(array('criteria' => $value));

通过在实体上调用方法是否可以实现相同的事情,例如:

$this->getUser()->getSharedNotes(array('criteria' => $value));

或者,有没有办法使用以前的 ORM 命令在多对多关系中进行过滤,例如:

$this->getDoctrine()->getRepository('PmbLicensingBundle:Note')->findBy(array('criteria' => $value, sharedUsers CONTAINS $this->getUser()));

摘自实体 User.php:

/**
 * User
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @Ser\ExclusionPolicy("all")
 */
class User implements AdvancedUserInterface, \Serializable
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Ser\Expose
     */
    protected $id;

    /**
      * @var User[]
      * 
      * @ORM\ManyToMany(targetEntity="Pmb\LicensingBundle\Entity\User", inversedBy="sharedNotes")
      * @Ser\Expose
     **/
    protected $sharedUsers;

    ...
}

摘自Entity User.php

/**
 * User
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @Ser\ExclusionPolicy("all")
 */
class User implements AdvancedUserInterface, \Serializable
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Ser\Expose
     */
    protected $id;


    /**
      * @var Note[]
      * 
      * @ORM\ManyToMany(targetEntity="Pmb\LicensingBundle\Entity\Note", mappedBy="sharedUsers")
      * @Ser\Expose
     **/
    protected $sharedNotes;

    ...
}

或者,对于 ManyToMany 或 OneToMany 属性,推荐的解决方案匹配标准是什么?我宁愿在不依赖实体管理器的情况下执行此操作。

【问题讨论】:

    标签: php symfony orm doctrine-orm


    【解决方案1】:

    您可以为此创建自定义查询。例如获取所有Notes 与用户$user 共享的内容:

        $em = $this->getDoctrine()->getManager();
        $notes = $em->getRepository('PmbLicensingBundle:Note')
            ->createQueryBuilder('Note')
            ->leftJoin('Note.sharedUsers', 'SharedUsers')
            ->where('SharedUsers = :user')
            ->setParameter('user', $user)
            ->getQuery()
            ->getResult()
            ;
    

    应该像这样执行查询:

    SELECT n0_.id AS id0 
    FROM notes n0_ 
    LEFT JOIN note_user n2_ ON n0_.id = n2_.note_id 
    LEFT JOIN users u1_ ON u1_.id = n2_.user_id 
    WHERE u1_.id = {userId}
    

    【讨论】:

      【解决方案2】:

      使用Criteria 类可以对集合进行过滤。

      $group          = $entityManager->find('Group', $groupId);
      $userCollection = $group->getUsers();
      
      $criteria = Criteria::create()
          ->where(Criteria::expr()->eq("birthday", "1982-02-17"))
          ->orderBy(array("username" => Criteria::ASC))
          ->setFirstResult(0)
          ->setMaxResults(20)
      ;
      
      $birthdayUsers = $userCollection->matching($criteria)
      

      ;

      详情请参阅http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html#filtering-collections

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 2013-12-11
        • 1970-01-01
        • 2017-07-14
        相关资源
        最近更新 更多