0

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

1 Answer 1

1

So i was finally able to solve this... Instead of using MappedSuperclass i changed all anotations to Document. Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

I also faced a similar issue, but my problem was not adding the * @MongoDB\InheritanceType("SINGLE_COLLECTION") for the base class.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.