Home » Php » Doctrine2 Postgresql and chat system (dialogs)

Doctrine2 Postgresql and chat system (dialogs)

Posted by: admin October 26, 2017 Leave a comment

Questions:

Entity

messages:
 id - primary
 author - ManyToOne User
 recipient - ManyToOne User
 createdAt - datetime

I can not think of a way to write a query that will output to me the latest messages that have been wrapped up in “dialogs”, where I am the author of the message or its recipient.

$qb = $this->em
            ->getRepository('AppBundle:Message')
            ->createQueryBuilder('message')
            ->addSelect('author')
            ->addSelect('recipient')
            ->join('message.author', 'author')
            ->join('message.recipient', 'recipient')
            ->where('message.author = :person OR message.recipient = :person')
            ->setParameter('person', $person)
            ->orderBy('message.createdAt', 'DESC')
            ->setMaxResults(self::DIALOGS_FOR_PAGE);

How can I add a group with the saving of sorting?

Grouping should be in the dialogues example (->addGropBy(‘author.id + recipient.id’))

Answers: