doumo0206 2018-08-14 13:24
浏览 46

带有值/ MongoDB / Symfony的Php数组

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计