0

Chat(id + info) User(id + some info) と関係テーブル chat_invited(chatId+userId) の 3 つのテーブルがあります。

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 に答える