Hey guys I have a little problem I will try to explain you (is not a technic problem just logic) :
So I have an mongoDB database and I send and receive message (I am doing a message system from user to user) and when I try to see the message and I am doing this for an API.
The problem is that when I do the post method I have to pass as parameter this :
{
"id_transmitter": 2,
"token": "eyJ0...",
"id_receiver": 4
}
Response :
{
"success": "true",
"message": [
[
{
"id": "5b71861fe138231d423a95ef",
"content": "5",
"transmitter": 2,
"Receiver": 4,
"send_at": {
"sec": 1534166559,
"inc": 1
}
}
],
[
{
"id": "5b71861ce138231d423a95ee",
"content": "4",
"transmitter": 2,
"Receiver": 4,
"send_at": {
"sec": 1534166556,
"inc": 1
}
}
],
[
{
"id": "5b718619e138231d423a95ed",
"content": "3",
"transmitter": 2,
"Receiver": 4,
"send_at": {
"sec": 1534166553,
"inc": 1
}
}
],
[
{
"id": "5b718615e138231d423a95ec",
"content": "2",
"transmitter": 2,
"Receiver": 4,
"send_at": {
"sec": 1534166549,
"inc": 1
}
}
]
],
"more": 1
}
As you can see I pass the receiver and the message to see and here is the problem I show only the message that we received OR that we send but not both at same time ... or I have to do 2 repository one with all received and one other with sended and when I will try to show by order of date that will print first one repo and after the other ... and not all together like a converstion should be ...
Here you can find how i did :
/**
*
* @Rest\Post(
* path = "/message/user/list/{page}",
* name = "api_message_list"
* )
*/
public function UserSeeMessageAction(Request $request, $page)
{
$dm = $this->get('doctrine_mongodb');
$messages = $dm->getRepository('AppBundle:MessageUser')->findBy(array
(
'idTransmitter' => ($request->get('id_transmitter')),
'idReceiver' => ($request->get('id_receiver')),
), array('id' => 'DESC'));
//return new JsonResponse([
// 'success' => "true",
// 'message' => $messages,
// 'more' => 0,
//]);
if (($request->get('id_receiver')) == null) {
return new JsonResponse([
'success' => "false",
'message' => "Receveur non renseigné"
]);
}
if (($request->get('id_transmitter')) == null) {
return new JsonResponse([
'success' => "false",
'message' => "Transmetteur non renseigné"
]);
}
if (!isset($messages)) {
return new JsonResponse([
'success' => "false",
'message' => "Pas de messages pour les utilisateurs"
]);
}
$arrayCollection = array();
$entities = 4;
$y = 0;
$x = 0;
foreach($messages as $message) {
if ($y >= ($page * $entities)) {
return new JsonResponse([
'success' => "true",
'message' => $arrayCollection,
'more' => 1,
]);
}
if ($y >= (($page - 1) * $entities) and $y < (($page) * $entities)) {
$x = 1;
$arrayCollection[] = array(
$message,
);
}
$y += 1;
}
if ($x == 1) {
return new JsonResponse([
'success' => "true",
'message' => $arrayCollection,
'more' => 0,
]);
}
return new JsonResponse([
'success' => "false",
'message' => "Plus de message",
]);
}
So for resume : my problem is that I print only sended message or received but not all together
Exemple :
Like it should be :
Hello i am one (send by one)
Hello one ! I am two (send by two)
How are you (send by one)
Fine (send by two)
Like i have :
Hello i am one (send by one)
How are you (send by one)
Hello one ! I am two (send by two)
Fine (send by two)
Like my problem is that i don't know how to set two repo in one .. i want a repo with the messages received and sended
Thx for all that will try to help ! I hope that you understand my bad explanation :)
Nb : The entity :
<?php
namespace AppBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document()
*/
class MessageUser implements \JsonSerializable
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\Field(type="string")
*/
protected $content;
/**
* @MongoDB\Field(type="timestamp")
*/
protected $sendAt;
/**
* @MongoDB\Field(type="boolean")
*/
protected $read;
/**
* @MongoDB\Field(type="integer")
*/
protected $idTransmitter;
/**
* @MongoDB\Field(type="integer")
*/
protected $idReceiver;
/**
* Get id
*
* @return $id
*/
public function getId()
{
return $this->id;
}
/**
* Set content
*
* @param string $content
* @return $this
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string $content
*/
public function getContent()
{
return $this->content;
}
/**
* Set sendAt
*
* @param $sendAt
* @return $this
*/
public function setSendAt($sendAt)
{
$this->sendAt = $sendAt;
return $this;
}
/**
* Get sendAt
*
* @return $sendAt
*/
public function getSendAt()
{
return $this->sendAt;
}
/**
* Set read
*
* @param boolean $read
* @return $this
*/
public function setRead($read)
{
$this->read = $read;
return $this;
}
/**
* Get read
*
* @return boolean $read
*/
public function getRead()
{
return $this->read;
}
/**
* Set idTransmitter
*
* @param integer $idTransmitter
* @return $this
*/
public function setIdTransmitter($idTransmitter)
{
$this->idTransmitter = $idTransmitter;
return $this;
}
/**
* Get idTransmitter
*
* @return integer $idTransmitter
*/
public function getIdTransmitter()
{
return $this->idTransmitter;
}
/**
* Set idReceiver
*
* @param integer $idReceiver
* @return $this
*/
public function setIdReceiver($idReceiver)
{
$this->idReceiver = $idReceiver;
return $this;
}
/**
* Get idReceiver
*
* @return integer $idReceiver
*/
public function getIdReceiver()
{
return $this->idReceiver;
}
function jsonSerialize()
{
return array(
"id" => $this->id,
"content" => $this->content,
"transmitter" => $this->idTransmitter,
"Receiver" => $this->idReceiver,
"send_at" => $this->sendAt,
);
}
}