public function setUpRelationalModel() { $parentDocs = [['username' => 'sam', 'job_title' => 'awesome guy'], ['username' => 'john', 'job_title' => 'co-awesome guy'], ['username' => 'dan', 'job_title' => 'programmer'], ['username' => 'lewis', 'job_title' => 'programmer'], ['username' => 'ant', 'job_title' => 'programmer']]; $childDocs = [['name' => 'jogging'], ['name' => 'computers'], ['name' => 'biking'], ['name' => 'drinking'], ['name' => 'partying'], ['name' => 'cars']]; $docsLinkedByDBRef = [['name' => 'python'], ['name' => 'java'], ['name' => 'php'], ['name' => 'lisp'], ['name' => 'C'], ['name' => 'Objective C'], ['name' => 'C++'], ['name' => 'ruby']]; foreach ([['class' => 'Interest', 'data' => $childDocs], ['class' => 'Skill', 'data' => $docsLinkedByDBRef]] as $subgroup) { foreach ($subgroup['data'] as $doc) { $i = new $subgroup['class'](); foreach ($doc as $k => $v) { $i->{$k} = $v; } $this->assertTrue($i->save()); } } // Lets make sure those child docs actually went in $skills = Skill::model()->findAll(); $this->assertTrue(count($skills) > 0); $c = Interest::model()->count(); $this->assertTrue($c > 0); // Let's build an array of the all the _ids of the child docs $c = Interest::model()->findAll(); $interest_ids = []; foreach ($c as $row) { $interest_ids[] = $row->_id; } // Create the users with each doc having the value of the interest ids $user_ids = []; foreach ($parentDocs as $doc) { $u = new User(); foreach ($doc as $k => $v) { $u->{$k} = $v; } $u->interests = $interest_ids; //Let`s take random skill as primary for this user $primarySkill = $skills[array_rand($skills)]->_id; $u->mainSkill = EMongoDBRef::create(Skill::model()->collectionName(), $primarySkill); //Now, lets pick array of secondary skills $allSecondarySkills = []; foreach ($skills as $i => $secondarySkill) { if ($secondarySkill->_id == $primarySkill) { continue; } array_push($allSecondarySkills, EMongoDBRef::create(Skill::model()->collectionName(), $secondarySkill->_id)); } $u->otherSkills = $allSecondarySkills; $this->assertTrue($u->save()); $user_ids[] = $u->_id; } $interests = $c; // Now 50^6 times re-insert each interest with a parnt user _id // So we have two forms of the document in interests, one without the parent user and one with for ($i = 0; $i < 50; $i++) { $randInt = rand(0, sizeof($interests) - 1); $row = $interests[$randInt]; $randPos = rand(0, sizeof($user_ids) - 1); $row->i_id = $user_ids[$randPos]; $row->setIsNewRecord(true); $row->setScenario('insert'); $this->assertTrue($row->save()); } // we will assume the set up was successful and we will leave it to further testing to see // whether it really was. }
/** * @param mixed $reference Reference to populate * @param null|string $cname Class of model to populate. If not specified, populates data on current model * @return EMongoModel */ public function populateReference($reference, $cname = null) { $row = EMongoDBRef::get(self::$db->getDB(), $reference); $o = is_null($cname) ? $this : $cname::model(); return $o->populateRecord($row); }