本文整理汇总了PHP中MongoDBRef类的典型用法代码示例。如果您正苦于以下问题:PHP MongoDBRef类的具体用法?PHP MongoDBRef怎么用?PHP MongoDBRef使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MongoDBRef类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testCreate
public function testCreate()
{
$collection = "d";
$id = 123;
$db = "phpunit_temp";
$ref = MongoDBRef::create($collection, $id);
$this->assertEquals("d", $ref['$ref'], json_encode($ref));
$this->assertEquals(123, $ref['$id'], json_encode($ref));
$this->assertArrayNotHasKey('$db', $ref, json_encode($ref));
$ref = MongoDBRef::create($collection, $id, $db);
$this->assertEquals("d", $ref['$ref'], json_encode($ref));
$this->assertEquals(123, $ref['$id'], json_encode($ref));
$this->assertEquals("phpunit_temp", $ref['$db'], json_encode($ref));
// test converting to strings
$ref = MongoDBRef::create(1, 2, 3);
$this->assertEquals("1", $ref['$ref'], json_encode($ref));
$this->assertEquals(2, $ref['$id'], json_encode($ref));
$this->assertEquals("3", $ref['$db'], json_encode($ref));
// more for tracking this behavior than condoning it...
$one = 1;
$two = 2;
$three = 3;
$ref = MongoDBRef::create(1, 2, 3);
$this->assertEquals("1", $one);
$this->assertEquals(2, $two);
$this->assertEquals("3", $three);
}
示例2: InsertCollection
public function InsertCollection($obj, $id)
{
//Insert obj values into Collection
if (!is_null($obj) || !is_null($this->Collect)) {
$this->Collect->remove();
}
if (!is_null($id)) {
$obj['_id'] = $id;
}
echo $obj["Thing"];
$Recipe = $obj["Recipe"];
$RecipeCollection = $this->dbObj->selectCollection("RecipeTest");
$RecipeCollection->Insert($Recipe);
//echo $Recipe['_id'].'\n';
$RecipeRef = MongoDBRef::create($RecipeCollection->getName(), $Recipe['_id']);
$CreativeWork = $obj["CreativeWork"];
$CreativeWork["RecipeReference"] = $RecipeRef;
$CreativeWorkCollection = $this->dbObj->selectCollection("CreativeWorkTest");
$CreativeWorkCollection->Insert($CreativeWork);
//echo $CreativeWork['_id'];
$CreativeWrokRef = MongoDBRef::create($CreativeWorkCollection->getName(), $CreativeWork['_id']);
$thing = $obj["Thing"];
$thing["CreativeWorkRef"] = $CreativeWrokRef;
$thingCollection = $this->dbObj->selectCollection("Thingtest");
$thingCollection->Insert($thing);
//Back ref
$recipeback = $this->dbObj->RecipeTest;
$RecipeResult = $recipeback->findOne(array("ingredients" => "suth"));
echo 'Result' . $RecipeResult['_id'];
print_r($RecipeResult);
$CWback = $this->dbObj->CreativeWorkTest;
//$CWbackResult = MongoDBRef::get($CWback->db, $RecipeResult['_id']);
$CWbackResult = $CWback->findOne(array("about" => "test1"));
print_r($CWbackResult);
}
示例3: testShouldReturnLazyLoadingCursor
public function testShouldReturnLazyLoadingCursor()
{
$parentCategory = new Category();
$parentCategory->setName('Parent category');
$parentCategory->setDesc('Parent category');
$parentCategory->save();
for ($i = 0; $i < 10; $i++) {
$category = new Category();
$category->setName('Category ' . $i);
$category->setDesc('Category ' . $i . ' desc');
$category->setCategory($parentCategory);
$category->save();
}
Category::enableLazyLoading();
$categories = Category::find([['category' => ['$ne' => null]]]);
$this->assertInstanceOf('\\Vegas\\Odm\\Collection\\LazyLoadingCursor', $categories);
foreach ($categories as $category) {
$this->assertInstanceOf('\\Fixtures\\Collection\\Category', $category);
$this->assertInstanceOf('\\Fixtures\\Collection\\Category', $category->getCategory());
}
$categories = Category::find([['category' => ['$ne' => null]]]);
$this->assertInstanceOf('\\Vegas\\Odm\\Collection\\LazyLoadingCursor', $categories);
foreach ($categories as $category) {
$this->assertInstanceOf('\\Fixtures\\Collection\\Category', $category);
$reflectionClass = new \ReflectionClass(get_class($category));
$categoryProperty = $reflectionClass->getProperty('category');
$categoryProperty->setAccessible(true);
$this->assertTrue(\MongoDBRef::isRef($categoryProperty->getValue($category)));
}
}
示例4: isRef
/**
* Returns true if the value passed appears to be a Mongo database reference
*
* @param mixed $obj
* @return boolean
**/
static function isRef($value)
{
if (!is_array($value)) {
return false;
}
return MongoDBRef::isRef($value);
}
示例5: update
/**
* method to convert plans names into their refs
* triggered before save the rate entity for edit
*
* @param Mongodloid collection $collection
* @param array $data
*
* @return void
* @todo move to model
*/
public function update($data)
{
if (isset($data['rates'])) {
$plansColl = Billrun_Factory::db()->plansCollection();
$currentDate = new MongoDate();
$rates = $data['rates'];
//convert plans
foreach ($rates as &$rate) {
if (isset($rate['plans'])) {
$sourcePlans = (array) $rate['plans'];
// this is array of strings (retreive from client)
$newRefPlans = array();
// this will be the new array of DBRefs
unset($rate['plans']);
foreach ($sourcePlans as &$plan) {
if (MongoDBRef::isRef($plan)) {
$newRefPlans[] = $plan;
} else {
$planEntity = $plansColl->query('name', $plan)->lessEq('from', $currentDate)->greaterEq('to', $currentDate)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref'))->current();
$newRefPlans[] = $planEntity->createRef($plansColl);
}
}
$rate['plans'] = $newRefPlans;
}
}
$data['rates'] = $rates;
}
return parent::update($data);
}
示例6: getReference
public function getReference()
{
$collection = collection::forClass($this->_className)->select();
if (null != $this->_model) {
$this->_model->save();
}
$id = $this->getId();
return \MongoDBRef::create($collection->getName(), $id, (string) $collection->db);
}
示例7: testRef
public function testRef()
{
ini_set("mongo.cmd", ":");
$this->object->insert(array("_id" => 123, "hello" => "world"));
$this->object->insert(array("_id" => 456, "ref" => array(":ref" => "bar", ":id" => 123)));
$ref = $this->object->findOne(array("_id" => 456));
$obj = MongoDBRef::get($this->object->db, $ref["ref"]);
$this->assertNotNull($obj);
$this->assertEquals("world", $obj["hello"], json_encode($obj));
}
示例8: __construct
function __construct(Glutton $glutton, $data = null, AxonCollection $parent = null, $position = null)
{
$this->_glutton = $glutton;
if (\MongoDBRef::isRef($data)) {
$this->_reference = Reader::simplifyReference($data);
$data = array_diff_key($data, $this->_reference);
}
$this->_elements = $data;
$this->_parent = $parent;
$this->_position = $position;
}
示例9: import
/**
* Import an account.
*
* @param string $username The username to use.
* @param string $password The password to use.
*/
public function import($username, $password)
{
$data = $this->get($username);
$this->db->remove(array('username' => $this->clean($username)));
$users = new users(ConnectionFactory::get('mongo'));
$id = $users->create($username, $password, $data['email'], $data['hideEmail'], $this->groups[$data['mgroup']], true);
$newRef = MongoDBRef::create('users', $id);
$oldRef = MongoDBRef::create('unimportedUsers', $data['_id']);
$this->mongo->news->update(array('user' => $oldRef), array('$set' => array('user' => $newRef)));
$this->mongo->articles->update(array('user' => $oldRef), array('$set' => array('user' => $newRef)));
self::ApcPurge('get', $data['_id']);
}
示例10: populateDb
public function populateDb()
{
$this->_users = array('bob' => array('_id' => new MongoId('4c04516a1f5f5e21361e3ab0'), '_type' => array('My_ShantyMongo_Teacher', 'My_ShantyMongo_User'), 'name' => array('first' => 'Bob', 'last' => 'Jones'), 'addresses' => array(array('street' => '19 Hill St', 'suburb' => 'Brisbane', 'state' => 'QLD', 'postcode' => '4000', 'country' => 'Australia'), array('street' => '742 Evergreen Terrace', 'suburb' => 'Springfield', 'state' => 'Nevada', 'postcode' => '89002', 'country' => 'USA')), 'friends' => array(MongoDBRef::create('user', new MongoId('4c04516f1f5f5e21361e3ab1')), MongoDBRef::create('user', new MongoId('4c0451791f5f5e21361e3ab2')), MongoDBRef::create('user', new MongoId('broken reference'))), 'faculty' => 'Maths', 'email' => 'bob.jones@domain.com', 'sex' => 'M', 'partner' => MongoDBRef::create('user', new MongoId('4c04516f1f5f5e21361e3ab1')), 'bestFriend' => MongoDBRef::create('user', new MongoId('4c0451791f5f5e21361e3ab2'))), 'cherry' => array('_id' => new MongoId('4c04516f1f5f5e21361e3ab1'), '_type' => array('My_ShantyMongo_Student', 'My_ShantyMongo_User'), 'name' => array('first' => 'Cherry', 'last' => 'Jones'), 'email' => 'cherry.jones@domain.com', 'sex' => 'F', 'concession' => true), 'roger' => array('_id' => new MongoId('4c0451791f5f5e21361e3ab2'), '_type' => array('My_ShantyMongo_ArtStudent', 'My_ShantyMongo_Student', 'My_ShantyMongo_User'), 'name' => array('first' => 'Roger', 'last' => 'Smith'), 'email' => 'roger.smith@domain.com', 'sex' => 'M', 'concession' => false));
$this->_userCollection = $this->_connection->selectDb(TESTS_SHANTY_MONGO_DB)->selectCollection('user');
foreach ($this->_users as $user) {
$this->_userCollection->insert($user, true);
}
$this->_articles = array('regular' => array('_id' => new MongoId('4c04516f1f5f5e21361e3ac1'), 'title' => 'How to use Shanty Mongo', 'author' => MongoDBRef::create('user', new MongoId('4c04516a1f5f5e21361e3ab0')), 'editor' => MongoDBRef::create('user', new MongoId('4c04516f1f5f5e21361e3ab1')), 'contributors' => array(MongoDBRef::create('user', new MongoId('4c04516f1f5f5e21361e3ab1')), MongoDBRef::create('user', new MongoId('4c0451791f5f5e21361e3ab2'))), 'relatedArticles' => array(MongoDBRef::create('article', new MongoId('4c04516f1f5f5e21361e3ac2'))), 'tags' => array('awesome', 'howto', 'mongodb')), 'broken' => array('_id' => new MongoId('4c04516f1f5f5e21361e3ac2'), 'title' => 'How to use Bend Space and Time', 'author' => MongoDBRef::create('user', new MongoId('broken_reference')), 'tags' => array('physics', 'hard', 'cool')));
$this->_articleCollection = $this->_connection->selectDb(TESTS_SHANTY_MONGO_DB)->selectCollection('article');
foreach ($this->_articles as $article) {
$this->_articleCollection->insert($article, true);
}
}
示例11: _val
/**
* Transform the value in a MongoDBRef if needed
* @param $attr
* @param $value
* @return mixed
*/
protected function _val($attr, $value)
{
$reference = \Rocketr\Schema\Reference::get($this->get_collection_name(), $attr);
$collection = \Rocketr\Schema\HasCollection::get($this->get_collection_name(), $attr);
if ($reference xor $collection) {
$ref_attr = \Rocketr\Schema\Map::on($this->get_collection_name(), $attr);
$_id = is_array($value) && isset($value[$ref_attr]) || is_object($value) && $value->{$ref_attr} ? is_array($value) ? $value[$ref_attr] : $value->{$ref_attr} : $value;
$target = $reference ? $reference : $collection;
$_id = $ref_attr == '_id' && \MongoId::isValid($_id) ? new \MongoId($_id) : $_id;
return \MongoDBRef::create($target, $_id);
} else {
return $value;
}
}
示例12: duplicate_rates
/**
* for every rate who has ref to original plan add ref to new plan
* @param type $source_id
* @param type $new_id
*/
public function duplicate_rates($source_id, $new_id)
{
$rates_col = Billrun_Factory::db()->ratesCollection();
$source_ref = MongoDBRef::create("plans", $source_id);
$dest_ref = MongoDBRef::create("plans", $new_id);
$usage_types = Billrun_Factory::config()->getConfigValue('admin_panel.line_usages');
foreach ($usage_types as $type => $string) {
$attribute = "rates." . $type . ".plans";
$query = array($attribute => $source_ref);
$update = array('$push' => array($attribute => $dest_ref));
$params = array("multiple" => 1);
$rates_col->update($query, $update, $params);
}
}
示例13: setReference
public function setReference($name, Base $object = null) {
if (!in_array($name, static::$_references)) {
throw new Exception("Document doesn't reference {$name}");
}
$this->_referenced[$name] = $object;
if ($object === null) {
$this->_data[$name] = null;
} else {
$class = get_class($object);
$this->_data[$name] = \MongoDBRef::create($class::collection()->getName(), $object->mongoId());
}
$this->_flagAttributeAsModified($name);
}
示例14: testExtendedDBRef
public function testExtendedDBRef()
{
$targetCollection = $this->getCollection('simpleTarget');
$targetId = new \MongoId();
$extends = [];
list($ref, $sourceDocument) = $this->createReferencedDocument($extends, true);
//extending by embedding some data
$ref['data'] = $sourceDocument['data'];
$targetDocument = ['_id' => $targetId, 'ref' => $ref, 'text' => 'amasource'];
$targetCollection->save($targetDocument);
$targetDocumentFromDB = $targetCollection->findOne(['_id' => $targetId]);
$this->assertTrue(\MongoDBRef::isRef($targetDocumentFromDB['ref']));
$sourceDocumentFromDB = $this->getSourceCollection()->getDBRef($targetDocumentFromDB['ref']);
$this->assertEquals($sourceDocument, $sourceDocumentFromDB);
$this->assertEquals($sourceDocumentFromDB['data'], $targetDocumentFromDB['ref']['data']);
}
示例15: readRef
/**
* Returns corresponding object indicated by MongoDBRef
*
* @param $fieldName
* @return mixed
* @throws InvalidReferenceException
*/
public function readRef($fieldName)
{
$oRef = $this->readNestedAttribute($fieldName);
if (!\MongoDBRef::isRef($oRef)) {
throw new InvalidReferenceException();
}
if (isset($this->dbRefs) && isset($this->dbRefs[$fieldName])) {
$modelInstance = $this->instantiateModel($this->dbRefs[$fieldName]);
} else {
if ($this->getDI()->has('mongoMapper')) {
$modelInstance = $this->getDI()->get('mongoMapper')->resolveModel($oRef['$ref']);
} else {
return $oRef;
}
}
return forward_static_call(array($modelInstance, 'findById'), $oRef['$id']);
}