本文整理汇总了PHP中MongoDBRef::create方法的典型用法代码示例。如果您正苦于以下问题:PHP MongoDBRef::create方法的具体用法?PHP MongoDBRef::create怎么用?PHP MongoDBRef::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoDBRef
的用法示例。
在下文中一共展示了MongoDBRef::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create
/**
* Creates a new database reference
*
* @param string|\Phalcon\Mvc\Collection $collection
* @param mixed|\MongoId|\Phalcon\Mvc\Collection $id
* @param string $database
* @return array
*/
public static function create($collection, $id = null, $database = null)
{
if ($collection instanceof \Phalcon\Mvc\Collection) {
$id = $collection->getId();
$collection = $collection->getSource();
}
if ($id instanceof \Phalcon\Mvc\Collection) {
$id = $id->getId();
}
if (is_array($collection) && self::isRef($collection)) {
if (isset($collection['$id'])) {
$id = $collection['$id'];
}
if (isset($collection['$ref'])) {
$collection = $collection['$ref'];
}
}
if (!$id instanceof \MongoId && $id !== null) {
$id = new \MongoId($id);
}
if ($collection instanceof \MongoId) {
return $collection;
}
if ($id === null) {
return null;
}
return parent::create($collection, $id, $database);
}
示例2: testReferences
public function testReferences()
{
/* This query return nothing currently */
/* but will save a reference */
$query = new Model1();
$query->limit(2, 1);
$query->sort('a DESC');
$c = new Model1();
$c->a = "foobar";
$c->save();
$ref = array('$ref' => 'model1', '$id' => $c->getID(), '$db' => DB, 'class' => 'Model1');
$this->assertEquals($c->getReference(), $ref);
$d = new Model1();
$d->a = "barfoo";
/* Reference into a document */
$d->next = $c;
/* References into sub documents */
$d->nested = array($c, $c);
/* MongoDBRef */
$d->mdbref = MongoDBRef::create('model1', $c->getID());
/* Get Dynamic query; AKA save the query */
/* in this case it would be a get all */
$d->query = $query->getReference(TRUE);
$d->save();
}
示例3: 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);
}
示例4: 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);
}
示例5: 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);
}
示例6: 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']);
}
示例7: 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);
}
}
示例8: _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;
}
}
示例9: 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);
}
}
示例10: create
/**
* Creates a new database reference
*
* @param string|\Phalcon\Mvc\Collection $collection
* @param mixed|\MongoId|\Phalcon\Mvc\Collection $id
* @param string $database
* @return array
*/
public static function create($collection, $id = null, $database = null)
{
if ($collection instanceof \Phalcon\Mvc\Collection) {
$id = $collection->getId();
$collection = $collection->getSource();
}
if ($id instanceof \Phalcon\Mvc\Collection) {
$id = $id->getId();
}
if (!$id instanceof \MongoId && $id !== null) {
$id = new \MongoId($id);
}
return parent::create($collection, $id, $database);
}
示例11: 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);
}
示例12: createReferencedDocument
public function createReferencedDocument($sourceDocument = null, $refIsFull = true)
{
$sourceCollection = $this->getSourceCollection();
$sourceId = new \MongoId();
$identity = ['_id' => $sourceId, 'data' => md5(rand(0, time()))];
if (is_array($sourceDocument)) {
$sourceDocument = array_merge($sourceDocument, $identity);
} else {
$sourceDocument = $identity;
}
$sourceCollection->save($sourceDocument);
if ($refIsFull) {
return [\MongoDBRef::create($sourceCollection->getName(), $sourceId, $sourceCollection->db), $sourceDocument];
} else {
return [\MongoDBRef::create($sourceCollection->getName(), $sourceId), $sourceDocument];
}
}
示例13: testConstruct
public function testConstruct()
{
$student = new My_ShantyMongo_Student();
$this->assertTrue($student->isNewDocument());
$this->assertTrue($student->isRootDocument());
$this->assertTrue($student->isConnected());
$this->assertTrue($student->hasKey());
$this->assertTrue($student->hasId());
$this->assertInternalType(PHPUnit_Framework_Constraint_IsType::TYPE_OBJECT, $student->getId());
$this->assertEquals('MongoId', get_class($student->getId()));
$this->assertEquals(My_ShantyMongo_Student::getCollectionRequirements(), $student->getRequirements());
$type = array('My_ShantyMongo_Student', 'My_ShantyMongo_User');
$this->assertEquals($type, $student->getInheritance());
$criteria = $student->getCriteria();
$this->assertTrue(array_key_exists('_id', $criteria));
$this->assertEquals($student->getId()->__toString(), $criteria['_id']->__toString());
$name = new My_ShantyMongo_Name();
$this->assertTrue($name->isNewDocument());
$this->assertTrue($name->isRootDocument());
$this->assertFalse($name->isConnected());
$this->assertFalse($name->hasKey());
$this->assertFalse($name->hasId());
$this->assertEquals(array(), $name->getInheritance());
$config = array('new' => false, 'connectionGroup' => 'default', 'db' => TESTS_SHANTY_MONGO_DB, 'collection' => 'user', 'pathToDocument' => 'name', 'requirementModifiers' => array('middle' => array('Required' => null)));
$name = new My_ShantyMongo_Name(array('first' => 'Jerry'), $config);
$this->assertFalse($name->isNewDocument());
$this->assertFalse($name->isRootDocument());
$this->assertTrue($name->isConnected());
$this->assertEquals($name->first, 'Jerry');
$requirements = array('_id' => array('Validator:MongoId' => null), '_type' => array('Array' => null), 'first' => array('Required' => null), 'last' => array('Required' => null), 'middle' => array('Required' => null));
$this->assertEquals($requirements, $name->getRequirements());
// Test input data initialisation
$data = array('name' => array('first' => 'Jerry', 'last' => 'Springer'), 'addresses' => array(array('street' => '35 Sheep Lane', 'suburb' => 'Sheep Heaven', 'state' => 'New Zealand', 'postcode' => '2345', 'country' => 'New Zealand')), 'friends' => array(MongoDBRef::create('user', new MongoId('4c04516f1f5f5e21361e3ab1')), MongoDBRef::create('user', new MongoId('4c0451791f5f5e21361e3ab2'))));
$student = new My_ShantyMongo_Student($data);
$this->assertNotNull($student->name);
$this->assertEquals('My_ShantyMongo_Name', get_class($student->name));
$this->assertEquals('Sheep Heaven', $student->addresses[0]->suburb);
$this->assertEquals('My_ShantyMongo_ArtStudent', get_class($student->friends[1]));
}
示例14: setUp
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*
* @access protected
*/
protected function setUp()
{
if (file_exists('tests/classic.jpg')) {
unlink('tests/classic.jpg');
}
$db = $this->sharedFixture->selectDB('phpunit');
$grid = $db->getGridFS('_files', '_chunks');
$grid->drop();
$files = $db->selectCollection('_files');
$chunks = $db->selectCollection('_chunks');
$chunk4 = array('cn' => 4, 'data' => new MongoBinData('xyz'));
$chunks->insert($chunk4);
$chunk3 = array('cn' => 3, 'data' => new MongoBinData('rst'), 'next' => MongoDBRef::create($chunks->getName(), $chunk4['_id']));
$chunks->insert($chunk3);
$chunk2 = array('cn' => 2, 'data' => new MongoBinData('lmno'), 'next' => MongoDBRef::create($chunks->getName(), $chunk3['_id']));
$chunks->insert($chunk2);
$chunk1 = array('cn' => 1, 'data' => new MongoBinData('abc'), 'next' => MongoDBRef::create($chunks->getName(), $chunk2['_id']));
$chunks->insert($chunk1);
$files->insert(array("filename" => "classic.jpg", "contentType" => "image/jpeg", "length" => 4, "chunkSize" => 4, "next" => MongoDBRef::create($chunks->getName(), $chunk1['_id'])));
$file = $grid->findOne();
$this->object = new MongoGridFSFileClassic($file);
$this->object->start = memory_get_usage(true);
}
示例15: createDBRef
/**
* Creates a database reference
*
* @link http://www.php.net/manual/en/mongocollection.createdbref.php
* @param array|object $document_or_id Object to which to create a reference.
* @return array Returns a database reference array.
*/
public function createDBRef($document_or_id)
{
if ($document_or_id instanceof \MongoId) {
$id = $document_or_id;
} elseif (is_object($document_or_id)) {
if (!isset($document_or_id->_id)) {
return null;
}
$id = $document_or_id->_id;
} elseif (is_array($document_or_id)) {
if (!isset($document_or_id['_id'])) {
return null;
}
$id = $document_or_id['_id'];
} else {
$id = $document_or_id;
}
return MongoDBRef::create($this->name, $id);
}