In order to better isolate the problem i tried to simplify my code, here it is:
This Document Trait is used as a 'generic' mongo document. I basically don't want to be rewritting $id and $createdAt in every document
/**
* Class Document
* @package TMPBundle\MongoDocument
* @MongoDB\MappedSuperclass()
*/
trait Document {
/**
* @MongoDB\Id()
*/
protected $id;
/**
* @MongoDB\Date()
*/
protected $createdAt;
}
Now the problem:
This class represents a generic element(a question) in a survey.The only data i need to persist is on this class ( but in the future i may need to add data to subclasses)
/**
* @MongoDB\MappedSuperclass()
* @MongoDB\InheritanceType("SINGLE_COLLECTION")
* @MongoDB\DiscriminatorField(fieldName="type")
* @MongoDB\Collection(name="survey_elements")
*/
abstract class AbstractSurveyType {
use Document;
/**
* @MongoDB\String()
*/
public $question;
/**
* @MongoDB\Collection()
*/
public $value;
/**
* @MongoDB\Bool()
*/
public $required;
/**
* @MongoDB\Bool()
*/
public $hasRemark;
/**
* @MongoDB\String()
*/
public $remarkQuestion;
}
This class represents a question that only has one choice as an answer ( a text input, a file input, etc).
/**
* Class SingleSurveyType
* @package TMPBundle\MongoDocument\Survey\SurveyTypes\SingleSurveyType
* @MongoDB\Document()
*/
abstract class SingleSurveyType extends AbstractSurveyType{
public function validate(){
if (count($this->value) != 1){
throw new InvalidSurveyElement("A SingleSurveyType cannot have more than one available value");
}
}
}
And finally i have my question with some extra methods:
/**
* Class FileSurveyType
* @package TMPBundle\MongoDocument\Survey\SurveyTypes\SingleSurveyType\Types
* @MongoDB\Document()
*/
class FileSurveyType extends SingleSurveyType{
protected function getFieldName()
{
return 'survey_question_file';
}
protected function getFieldType()
{
return 'file';
}
}
In my controller when i'm just doing this:
public function submitSurveyAction(Request $request){
$file = new FileSurveyType();
$file->setValue(["algo"]);
$file->setQuestion("questao");
$this->get('doctrine_mongodb.odm.document_manager')->persist($file);
$this->get('doctrine_mongodb.odm.document_manager')->flush();
}
After running the controller Action the desired fields but if i set the properties in AbstractSurveyType as protected the fields are not persisted. Also, after running ´php app/console doctrine:mongodb:generate:documents´ for the first time (with public modified) i get this error:
´Access level to SingleSurveyType::$question must be public (as in class AbstractSurveyType)
My Initial Question:
I'm currently using doctrine in symfony2 to persist data to a MongoDB database. my project is just a form/survey creation system.I have several question types , each question type can be a singleType (text,number) or multipleType (choices, checkboxes,etc). I'm trying to save some documents using inheritance. This is a small set of what i have:
trait Document { /** * @MongoDB\Id() */ protected $id; /** * @MongoDB\Date() */ protected $createdAt; }Then i use this
Documentin all documents ( so that i don't have to keep repeating the id property and the updatedAt )* @MongoDB\MappedSuperclass() * @MongoDB\DiscriminatorField("type") * @MongoDB\InheritanceType("SINGLE_COLLECTION") */ abstract class AbstractSurveyType { /** * @MongoDB\String() */ protected $question; }Before reaching the final class i still have this one with only a couple of methods ( single and multiple types are not handled in the same way)
/** * Class SingleSurveyType * @MongoDB\MappedSuperclass() * @MongoDB\InheritanceType("SINGLE_COLLECTION") */ abstract class SingleSurveyType extends AbstractSurveyType{ }And finally i have my type:
/** * @MongoDB\Document() * @package TMPBundle\MongoDocument\Survey\SurveyTypes\SingleSurveyType\Types */ class NumberSurveyType extends SingleSurveyType{ use Document; }The problem i'm having is that when i persist the data to the database the properties inherited from Document, AbstracSurveyType and SingleSurveyType do not get persisted. My documents only get a _id and type properties... Is this supposed to happend?
Thanks in advance :)