donglian3055 2014-06-23 11:08
浏览 43

Symfony2过滤器控制器中的相关实体

I have an entity "Note", that links to my entity "User" in a ManyToMany Relationship. From my User entity, I want to get all Notes that match certain criteria.

I know the via ORM you can do this:

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

Is the same thing possible by invoking a method on an entity, for instance:

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

Alternatively, is there a way to use the former ORM command to filter within a ManyToMany relationship, for instance:

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

Excerpt from 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 User[]
      * 
      * @ORM\ManyToMany(targetEntity="Pmb\LicensingBundle\Entity\User", inversedBy="sharedNotes")
      * @Ser\Expose
     **/
    protected $sharedUsers;

    ...
}

Excerpt from 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;

    ...
}

Alternatively, what is the recommended solution matching criteria on a ManyToMany or OneToMany property? I would prefer to execute this without relying on an Entity Manager.

  • 写回答

2条回答 默认 最新

  • dshxbrzgi090608692 2014-06-23 11:43
    关注

    You can create custom query for that. For example to get all Notes share with user $user write:

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

    That should execute query like this:

    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}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端