I am vary new to mongo and doctrine ODM but not ORM. I have a marquees document and a problems document. I would like problems to exist as their own collection, but when saving a marquee being able to attach a problem to the marquee. I would then like to be able to fetch a marquee by its id and have the problems document as data in the marquee.
/**
* Marquees
*
* @ODM\Document(collection="marquees", repositoryClass="MarqueesRepository")
*/
class Marquees {
/** @ODM\Id(strategy="AUTO") */
protected $id;
/** @ODM\ReferenceMany(targetDocument="Problems", cascade="all") */
protected $problems;
public function addProblems(Problems $problem) {
$this->problems[] = $problem;
}
}
I also have Problems document
/**
* Problems
*
* @ODM\Document(collection="problems", repositoryClass="ProblemsRepository")
*/
class Problems {
/** @ODM\Id(strategy="AUTO") */
protected $id;
/** @ODM\ReferenceOne(targetDocument="Marquees") */
protected $marquee;
/** @ODM\Field(type="string") */
protected $problem;
/** @ODM\Field(type="string") */
protected $description;
public function setProblem($problem) {
$this->problem = $problem;
}
}
I create a new marquee with a problem saved with it and this seems right.
$marquee = new Marquees;
$this->odm->persist($marquee);
$class = new \Watch\IdeasFactory\Documents\Problems;
$class->setProblem($data->problems['problem']);
$marquee->addProblems($class);
var_dump($this->odm->flush());die();
> db.marquees.find().pretty()
{
"_id" : ObjectId("55008c95b5c14186118b4567"),
"problems" : [
DBRef("problems", ObjectId("55008c95b5c14186118b4568"))
]
}
> db.problems.find().pretty()
{ "_id" : ObjectId("55008c95b5c14186118b4568"), "problem" : "testing" }
Next I want to fetch this marquee by its id and either export it or have a hydrated array with both documents. But this is where I'm having trouble. I can fetch the marquee by using QueryBuilder and setting hydrating to false, but it just gives me an array without the referenced document problems.
public function fetchById($id) {
$marquees = $this->createQueryBuilder('Marquees')
->field('id')->equals($id)
->hydrate(false)
->getQuery()
->getSingleResult();
}
array (
'_id' =>
MongoId::__set_state(array(
'$id' => '55008c95b5c14186118b4567',
)),
'problems' =>
array (
0 =>
array (
'$ref' => 'problems',
'$id' =>
MongoId::__set_state(array(
'$id' => '55008c95b5c14186118b4568',
)),
'$db' => 'watch_my_idea',
),
),
)
If I remove the hydrate(false) or just use find($id) I get back a Marquee, but then whats the best way to hydrate the marquee with the problem documents? And this seems to get hairy quick because then it seems that any reference on a document would need to manually be hydrated (because I dont think odm will do it recursively) and maintaining that is a problem. I have read about the prime(true) method, but I must not be using it right because I still only get ref ids back even after priming.
If I use prime(true) this will force me also to iterate unless there is something else I'm missing.
$problems = $marquees->getProblems();
foreach($problems as $problem) {
var_dump($problem);die();
}
Am I going to have to manually hydrate all of this or is there some way that is way better than this. I'm following a standard 1 route to 1 resource. So even though I want the problems data with the marquee with find($id), I will not be updating a problem on a marquee route. Instead the plan is to use a problem route to update problems. This is for display data only on find($id) route. Any help is appreciated