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 :)