0

我有 3 个表 Chat(id + info) User(id + some info) 和关系表 chat_invited(chatId+userId)

我正在使用 ZF2 和 Doctrine2

我有实体聊天:

<?php

namespace Chat\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Chat
 *
 * @ORM\Table(name="chat")
 * @ORM\Entity
 */
class Chat
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=256, nullable=true)
     */
    private $title;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    private $created;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Chat\Entity\User", inversedBy="chat")
     * @ORM\JoinTable(name="chat_invited",
     *   joinColumns={
     *     @ORM\JoinColumn(name="chat", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="user", referencedColumnName="id")
     *   }
     * )
     */
    private $user;

    /**
     * @var \Chat\Entity\ChatCategory
     *
     * @ORM\ManyToOne(targetEntity="Chat\Entity\ChatCategory")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="category", referencedColumnName="id")
     * })
     */
    private $category;

    /**
     * @var \Chat\Entity\User
     *
     * @ORM\ManyToOne(targetEntity="Chat\Entity\User")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="owner", referencedColumnName="id")
     * })
     */
    private $owner;

    /**
     * @var \Chat\Entity\Building
     *
     * @ORM\ManyToOne(targetEntity="Chat\Entity\Building")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="building", referencedColumnName="id")
     * })
     */
    private $building;

    /**
     * @var \Chat\Entity\Company
     *
     * @ORM\ManyToOne(targetEntity="Chat\Entity\Company")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="company", referencedColumnName="id")
     * })
     */
    private $company;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->user = new \Doctrine\Common\Collections\ArrayCollection();
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add user
     *
     * @param \Chat\Entity\User $user
     * @return Chat
     */
    public function addUser(\Chat\Entity\User $user)
    {
        $this->user[] = $user;

        return $this;
    }

    /**
     * Remove user
     *
     * @param \Chat\Entity\User $user
     */
    public function removeUser(\Chat\Entity\User $user)
    {
        $this->user->removeElement($user);
    }

    /**
     * Get user
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getUser()
    {
        return $this->user;
    }


........................................



} 

如何找到邀请了 User1 和 User2 的聊天?

4

1 回答 1

0

它对我有用

$qb = $em->createQueryBuilder();
            $query = $qb->select('chats, user')
                ->from('Chat\Entity\Chat', 'chats')
                ->join('chats.user', 'user')
                ->join('chats.user', 'user2')
                ->where('user.id = :user AND user2.id = :inv ')
                ->setParameter('user', <User1.id>)
                ->setParameter('inv',<User2.id>)
                ->setMaxResults(1)
                ->getQuery();
于 2013-08-23T13:02:41.283 回答