简体   繁体   中英

Symfony private messaging system

I come to you because I need help, right now I create an ad site what I want is to create a private messaging system that is to say, there is 2 users who can speak this and if for example a 3rd finds the url and come on the url he can not write a message or others and all that, without going through a third party bundle, I want to do everything myself

I have an entity ads, users, and finally message

I have relationships like this:

user => message : OneToMany Advertisement => message OneToMany

so my question how to limit the route to 2 person only and how to display their messages?

should I change my relationship? How can I handle this in my controller?

I share my code a little controller :

/**
     * @Route("/messages/{id}", name="messages")
     * @param int $id
     * @return Response
     * @throws \Exception
     */
    public function messagesShow(int $id): Response {

        $user = $this->getUser();
        $advertisement = $this->getDoctrine()->getRepository(Advertisement::class)->findByMessages($id);

        $messages = $this->getDoctrine()->getRepository(Message::class)->findByMessages($user, $advertisement);

        return $this->render('advertisement/messages.html.twig', [
            'test' => $messages,
            'advertisement' => $advertisement
        ]);
    }

and repository :

/**
 * @param int $id
 * @return Advertisement|null
 * @throws \Exception
 */
public function findByMessages(int $id): ?Advertisement {

    $query = $this->createQueryBuilder('a')
    ->select('a')
    ->from('App:Advertisement', 'r')
    ->where('a.id = :id')
    ->leftJoin('a.message', 'm')
    ->setParameter(':id', $id)
    ->getQuery()
    ;

    try {
        return $query->getOneOrNullResult();
    }
    catch (\Exception $e) {
        throw new \Exception('Problème' . $e->getMessage() . $e->getLine());
    }
}


/**
     * @param User $user
     * @param Advertisement $advertisement
     * @return Message|null
     * @throws \Exception
     */
    public function findByMessages(User $user, Advertisement $advertisement) {

    // SELECT * FROM `message` WHERE advertisement_id = 5 AND user_id = 1
        $query = $this->createQueryBuilder('m')
        ->select('m')
        ->from('App\Entity\Message', 'n')
        ->join('m.advertisement', 'a')
        ->where('m.advertisement = :advertisement')
        ->andWhere('m.user = :user')
        ->setParameter(':user', $user)
        ->setParameter(':advertisement', $advertisement)
        ->getQuery()
        ;

        try {
            return $query->getResult();
        }
        catch (\Exception $e) {
            throw new \Exception('Problème' . $e->getMessage() . $e->getLine());
        }
    }

Thanks for your help ! :D

Create a Conversation table that has a relation to ConversationUsers table, keeping in track which users are allowed to participate in the conversation. Conversation object should also keep track of Message objects belonging to that Conversation . Then you can simply load Conversation in your code and can 100% be sure that it will be only visible to the users, participating in it. 视觉(请原谅x-mind,因为我无法使用当前计算机上的适当工具...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM