0

in my project i'm using a document "Question" which references (Many to another document "Category") after setting the Category of a question and flushing nothing changes in my database there is my code

    $dm = $this->getDocumentManager();

    $question = $this->getDocumentManager()->getRepository('ATS\QuizzBundle\Document\Question')->findOneByQuestion("a?");
    $category = $this->getDocumentManager()->getRepository('ATS\QuizzBundle\Document\Category')->findOneByLabel("Logic");

    $question->addCategory($category);

    $dm->flush();

and there is no changes in my database, any one can help please ? and here is my mapping in the Question's Document :

 /**
 *@MongoDB\ReferenceMany(targetDocument="Category") 
*/
  protected $category 
Alouini Khalil
  • 655
  • 5
  • 16

1 Answers1

1

Depending on your ChangeTrackingPolicy you might need to persist the question before flushing.

 $dm->persist($question);
 $dm->flush();

By persisting an entity you make sure that any changes in the entity are registered in the UnitOfWork. With a flush you send these actions to the database.

Persist does not directly cause a query to the database since you might want to persist a lot of entities. Therefore all actions are 'buffered' until flush is called on the document manager.

Sander Toonen
  • 3,463
  • 35
  • 54
  • it is not the solution (if we get the question from the db it is not necessary to persist) i've tried id and did not work – Alouini Khalil Apr 14 '15 at 16:10
  • Am I correct that you already have a question and a category and all you want to do is add the category to the question? In that case, could you provide the mapping so we can see if anything is missing in the definition of the relation? – Sander Toonen Apr 14 '15 at 16:14
  • yes you're right Xatoo, i've edited the question to add my mapping.. i've tried with referenceOne and it works but don't with referenceMany. – Alouini Khalil Apr 15 '15 at 10:11