我有3个表聊天(id + info)用户(id +一些信息)和关系表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给他的聊天?
答案 0 :(得分: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();