1

Hey guys i am working with symfony 3.4 and i would like to know how can i do an array with differents types of values (in mongoDB for sure, i am using the bundle mongoDB for symfony)

Exemple that i want :

{
    "id_of_the_conversation": 367,
    "messages_in_the_conversation": [
        {
            "id": 1,
            "messages": "Hello i need help !",
            "send_by": 34,
            "received_by": 22
        }
        {
            "id": 2,
            "messages": "Stack stack",
            "send_by": 22,
            "received_by": 34
        }
        {
            "id": 3,
            "messages": "Maybe someone ?",
            "send_by": 34,
            "received_by": 22
        }
    ],
}

So as you can see messages_in_the_conversation is an array with different types. I don't want to use a relation manytoone because it will generate more documents, and i just want all the messages in the same document.

I see that there are different types like collection or hash but don't know how to use it for make an array with different types.

Thx for all that will help, i don't want that you do the job for me only tips will be sooo usefull, thx you all :)

Edit :

my entity : (not exactly the same with the exemple i tried a many to one relation)

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="integer")
 */
protected $idTransmitter;

/**
 * @MongoDB\Field(type="integer")
 */
protected $idReceiver;

/**
 * @MongoDB\Field(type="timestamp")
 */
protected $sendAt;

/**
 * @MongoDB\Field(type="boolean")
 */
protected $read;

/**
 * @MongoDB\ReferenceOne(targetDocument="ConversationDocument", mappedBy="messages")
 */
private $conversation;

/**
 * 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;
}

/**
 * @return mixed
 */
public function getConversation()
{
    return $this->conversation;
}

/**
 * @param mixed $conversation
 */
public function setConversation($conversation)
{
    $this->conversation = $conversation;
}

/**
 * @return mixed
 */
public function getIdTransmitter()
{
    return $this->idTransmitter;
}

/**
 * @param mixed $idTransmitter
 */
public function setIdTransmitter($idTransmitter)
{
    $this->idTransmitter = $idTransmitter;
}

/**
 * @return mixed
 */
public function getIdReceiver()
{
    return $this->idReceiver;
}

/**
 * @param mixed $idReceiver
 */
public function setIdReceiver($idReceiver)
{
    $this->idReceiver = $idReceiver;
}

function jsonSerialize()
{
    return array(
        "id"       => $this->id,
        "content" => $this->content,
        "send_at" => $this->sendAt,
        "sender" => $this->idTransmitter,
        "receiver" => $this->idReceiver,
        "id_conversation" => $this->conversation,
    );
}

}

When i create a many to one relation i have to do : $message = new MessageUser(); and that generate a new document you can see here i have one document for each message and conversation :

https://ibb.co/gUZS0p

18
  • How is this related to a database at all? Of course, you can build arrays like you want to Commented Aug 14, 2018 at 13:30
  • it's related with the entity thx to the bundle, so how can you do an array like this ? Commented Aug 14, 2018 at 13:33
  • /** * @MongoDB\Field(type="string") */ protected $content; for exemple Commented Aug 14, 2018 at 13:33
  • What is "the bundle"? Can you share your code? Commented Aug 14, 2018 at 13:37
  • here you have tell me if you need more things :) Commented Aug 14, 2018 at 13:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.