doujiang2641 2015-03-21 22:48
浏览 556

Mongodb中映射的动态实体定义

Edit: Still working on my problem. I finally used @MongoDB\EmbedDocument to save the complex Document and it seems to be working as expected. Sadly, the retrieving part is not working very well. If i retrieve a very basic element, it goes very well. But as far as i use a EmbedDocument, i am retrieving extra data and methods that seems to be created by recursion and do not stop.

Any idea ?


I am currently facing a design issue in using MongoDB with Symfony2. My aim is to create a polymorph class that could allow me to register a Document with properties that can change from a Document to another.

The idea is to use the MongoDB's schema-free approach to save object definitions that can be defined on-the fly.

So, I created an abstract class to define my properties Type. I'll extend all my properties from that AbstractEntity, and get some formated basic property (String, Int, ...).

/**
 * @MongoDB\MappedSuperclass
 */
abstract class AbstractEntity {

    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $name;

    /**
     * @MongoDB\String
    */
    protected $type;

    /**
     * Every basic element has an ID
     * @return @MongoDB\Id
     */
    public function getId() {
        return $this->id;
    }

    // Here are methods, getters and setters
    // ...
}

Then, I created a class Document that is able to be extended with some basic properties I described earlier. My Document class contains an array of properties.

/**
 * @MongoDB\Document
 */
class Document extends AbstractEntity{

    /**
     * @MongoDB\???
     */
    private $attributes = array();

    public function __construct($name) {
        $this->setType(TYPE::DOCUMENT);
        $this->setName($name);
    }

    public function addAttribute($type, $name, $options = array()) {

        $attribute = $this->attributeFactory($type, $name, $options);
        array_push($this->attributes, $attribute);
        return $this;
    }

    private function attributeFactory($type, $name, $options = array()) {

        $entity = NULL;

        if( $type == TYPE::STRING) {
            $entity = new String($name);
        }

        return $entity;
    }

}

From this work, i was hoping to be able to save in my database a single entry (a Document) that could describe a new document with its properties, something like:

{
    "name" : "Character",
    "type" : "Document",
    "properties" : [
        {
            "name": "alias",
            "type": "String"
        },
        // ...
    ]
}

Sadly, i am new on mongodb + symfony and i feel stuck because of the mapping, which is great but limited in an schema-free approach.

Does anyone know how i could make that working or if i am going in the wrong direction ?

In my idea, i would like to limit to split my document definition into a relational approach (not mongodb's spirit) from a document to multiple rows.

I am open to any suggestion or discussion :) Thanks :)

  • 写回答

1条回答 默认 最新

  • dpndp64206 2015-03-29 17:08
    关注

    I finally succeeded in saving my document using EmbededDocument and EmbedMany annotations. I still have an issue in getting the results, due to a recursion loop, but i will solve that in another post.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料