create() 공개 정적인 메소드

If no database is given, the current database is used.
public static create ( string $collection, mixed $id, string $database = null ) : array
$collection string Collection name (without the database name)
$id mixed The _id field of the object to which to link
$database string Database name
리턴 array Returns the reference
예제 #1
0
파일: DbRef.php 프로젝트: vegas-cmf/odm
 /**
  * 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
0
 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
0
 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);
 }
 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
0
 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
0
 /**
  * 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
0
 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' => '*****@*****.**', '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' => '*****@*****.**', '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' => '*****@*****.**', '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
0
파일: Writr.php 프로젝트: alex-robert/knot
 /**
  * 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
0
파일: Plans.php 프로젝트: ngchie/system
 /**
  * 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
0
파일: DbRef.php 프로젝트: arius86/core
 /**
  * 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
0
	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
0
 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
0
 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]));
 }
 /**
  * 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);
 }
 /**
  * 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);
 }
예제 #16
0
 /**
  * Create a dbref of this object if required
  * @return MongoDBRef
  */
 public function getDBRef()
 {
     return \MongoDBRef::create(static::$collection, $this->_id);
 }
예제 #17
0
파일: Model.php 프로젝트: noikiy/inovi
 /**
  * Create a Mongodb reference
  *
  * @return \MongoDBRef
  */
 public function makeRef()
 {
     $ref = \MongoDBRef::create($this->collectionName(), $this->getId());
     return $ref;
 }
예제 #18
0
 private function countParents($structureDo, $query)
 {
     $id = $query->getCondition();
     $filter = ['child' => \MongoDBRef::create('content', new \MongoId($id))];
     if (!$this->isInitialized($structureDo)) {
         $this->initialize($structureDo);
     }
     $mongoCollection = $this->db->selectCollection('relation');
     $cursor = $mongoCollection->find($filter);
     return $cursor->count();
     // db.relation.find({"child" : DBRef("content", ObjectId("5510618a06b13a931ca41c07"))}).pretty()
 }
예제 #19
0
 /**
  * Utility function that generate a 'MongoDBRef' ready for storage
  * 
  * @param   string          The collection the referenced document is in
  * @param   MongoID         The '_id' of the referenced document
  * @param   string          Optionally you can reference documents of another database on the server
  * @return  MongoDBRef
  * @access  public
  */
 public function gen_dbref($collection, $id, $db = null)
 {
     if ($db) {
         return MongoDBRef::create($collection, $id, $db);
     } else {
         return MongoDBRef::create($collection, $id);
     }
 }
예제 #20
0
 private function _makeReference($model)
 {
     $model = get_called_class();
     $ref = \MongoDBRef::create($this->collectionName(), $this->getId(), $this->dbName());
     return $ref;
 }
예제 #21
0
 /**
  * Create database reference.
  * 
  * Create mongo dbref object to store later
  * 
  * <code>
  * $ref = $this->mongo_db->create_dbref($collection, $id);
  * </code>
  *
  * @param string $collection Collection name
  * @param string $field      Field name
  * @param string $db_name    Database name
  *
  * @access public
  * @return array|object
  */
 public function create_dbref($collection = '', $field = '', $db_name = '')
 {
     if (empty($collection)) {
         $this->_show_error('In order to retrieve documents from MongoDB, a collection name must be passed', 500);
     }
     if (empty($field) || !isset($field)) {
         $this->_show_error('To use MongoDBRef::create() ala create_dbref() you must pass a valid field id of the object which to link', 500);
     }
     $database = $db_name !== '' ? $db_name : $this->_dbhandle;
     return MongoDBRef::create($collection, $field, $database);
 }
예제 #22
0
 public function testCreateWithDatabase()
 {
     $id = new \MongoId();
     $ref = \MongoDBRef::create('foo', $id, 'database');
     $this->assertSame(['$ref' => 'foo', '$id' => $id, '$db' => 'database'], $ref);
 }
예제 #23
0
 /**
  *	--------------------------------------------------------------------------------
  *	Create Database Reference
  *	--------------------------------------------------------------------------------
  *
  *	Create mongo dbref object to store later
  *
  *	@usage : $ref = $this->mongo_db->create_dbref($collection, $id);
  */
 public function create_dbref($collection = "", $id = "", $database = FALSE)
 {
     if (empty($collection)) {
         show_error("In order to retreive documents from MongoDB, a collection name must be passed", 500);
     }
     if (empty($id) or !isset($id)) {
         show_error('To use MongoDBRef::create() ala create_dbref() you must pass a valid id field of the object which to link', 500);
     }
     $db = $database ? $database : $this->db;
     if ($this->CI->config->item('mongo_return') == 'object') {
         return (object) MongoDBRef::create($collection, $id, $db);
     } else {
         return (array) MongoDBRef::create($collection, $id, $db);
     }
 }
예제 #24
0
 public function testCreateRef() {
     $ref = MongoDBRef::create("x", "y");
     $this->assertEquals('x', $ref['$ref']);
     $this->assertEquals('y', $ref['$id']);
 }
예제 #25
0
 /**
  * Test ExtReference::closureToPHP()
  *
  * @return void
  */
 public function testClosureToPHP()
 {
     $this->assertEqualsClosure(ExtReference::create(__METHOD__, __FILE__), \MongoDBRef::create(__METHOD__, __FILE__), $this->type->closureToPHP());
     $this->assertEqualsClosure(null, null, $this->type->closureToPHP());
 }
예제 #26
0
 /**
  * Test newly added documents to a document set
  */
 public function testSaveChildOfDocumentSet()
 {
     $address = $this->_bob->addresses->new();
     $address->street = '35 Sheep Lane';
     $address->suburb = 'Sheep Heaven';
     $address->state = 'New Zealand';
     $address->postcode = '2345';
     $address->country = 'New Zealand';
     $address->save();
     $bobRaw = 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'), 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')), MongoDBRef::create('user', new MongoId('broken reference'))), 'faculty' => 'Maths', 'email' => '*****@*****.**', 'sex' => 'M', 'partner' => MongoDBRef::create('user', new MongoId('4c04516f1f5f5e21361e3ab1')), 'bestFriend' => MongoDBRef::create('user', new MongoId('4c0451791f5f5e21361e3ab2')));
     $this->assertEquals($bobRaw, $this->_userCollection->findOne(array('_id' => new MongoId('4c04516a1f5f5e21361e3ab0'))));
 }
예제 #27
0
<?php

// Helper functions and includs
include_once '/var/www/html/Lux/Core/Helper.php';
$db = new Db("Inventory");
$OUTPUT = new Output();
$REQUEST = new Request();
$collection = $db->selectCollection("Cart");
$RULES = new Rules(1, "cart");
$REQUEST = new Request();
// get the asset, push it into the cart that is selected
$collectionName = $REQUEST->get("collection", "Standard");
$cartName = $REQUEST->get("wishlist", "Default");
$document = $collection->findAndModify(array("user_id" => $RULES->getId()), array('$push' => array("wishlist." . $cartName => MongoDBRef::create($collectionName, $REQUEST->get("id"), "Assets"))));
// Used for analytics
$LOG = new Logging("Cart.order");
$LOG->log($RULES->getId(), 43, $REQUEST->get("id"), 100, "User Wished for item");
$OUTPUT->success(0, $document, null);
 /**
  * Converts YML strings to native Mongo* objects. E.g. if a string is "MongoDate(123)", it will
  * be converted to a MongoDate object representing the Unix time "123", and "MongoDBRef(User,abc)"
  * will be converted to a MongoDBRef object representing the collection 'User' and ID 'abc'.
  * If an array is given, this method is applied recursively to all of the array's values.
  *
  * @param mixed $value
  * @return mixed
  */
 private function convertYmlStringToNativeMongoObjects($value)
 {
     if (is_array($value)) {
         return array_map([$this, 'convertYmlStringToNativeMongoObjects'], $value);
     } elseif (is_string($value)) {
         if (preg_match(self::$matchNativeMongoClass, $value, $matches) === 1) {
             if ($matches[1] === 'DBRef') {
                 list($collection, $id) = explode(',', $matches[2]);
                 return \MongoDBRef::create($collection, new \MongoId($id));
             } else {
                 $nativeMongoClass = 'Mongo' . $matches[1];
                 return new $nativeMongoClass($matches[2]);
             }
         }
         return $value;
     }
     return $value;
 }
예제 #29
0
 /**
  * Create a reference to this document
  * 
  * @return array
  */
 public function createReference()
 {
     if (!$this->isRootDocument()) {
         require_once 'Shanty/Mongo/Exception.php';
         throw new Shanty_Mongo_Exception('Can not create reference. Document is not a root document');
     }
     if (!$this->isConnected()) {
         require_once 'Shanty/Mongo/Exception.php';
         throw new Shanty_Mongo_Exception('Can not create reference. Document does not connected to a db and collection');
     }
     return MongoDBRef::create($this->getConfigAttribute('collection'), $this->getId());
 }
예제 #30
0
 public function validate($title, $category, $description, $text, $tags, $creating = true)
 {
     $ref = MongoDBRef::create('users', Session::getVar('_id'));
     $func = function ($value) {
         return trim($value);
     };
     $title = substr($this->clean($title), 0, 100);
     $description = substr($this->clean($description), 0, 500);
     $body = substr($this->clean($text), 0, 7000);
     if (is_array($tags)) {
         $tags = implode(',', $tags);
     }
     $tags = array_map($func, explode(',', $this->clean($tags)));
     if (empty($title)) {
         return 'Invalid title.';
     }
     if (empty($description)) {
         return 'Invalid description';
     }
     if (empty($body)) {
         return 'Invalid body.';
     }
     if (empty(self::$categories[$category])) {
         return 'Invalid category.';
     }
     $entry = array('title' => $title, 'category' => $category, 'description' => $description, 'body' => $body, 'tags' => $tags, 'user' => $ref, 'date' => time(), 'commentable' => true, 'published' => false, 'ghosted' => false);
     if (!$creating) {
         unset($entry['user'], $entry['date'], $entry['commentable'], $entry['published']);
     }
     self::ApcPurge('getNewPosts', null);
     return $entry;
 }