MongoDBRef::get PHP Method

get() public static method

Fetches the object pointed to by a reference
public static get ( MongoDB $db, array $ref ) : array | null
$db MongoDB Database to use
$ref array Reference to fetch
return array | null Returns the document to which the reference refers or null if the document does not exist (the reference is broken)
    public static function get($db, $ref)
    {
        if (!static::isRef($ref)) {
            return null;
        }
        return $db->selectCollection($ref[static::$refKey])->findOne(['_id' => $ref[static::$idKey]]);
    }

Usage Example

Example #1
0
 public function validate($contentId, $text, $creating = true)
 {
     $valid = false;
     foreach ($this->commentableCollections as $collection) {
         $ref = MongoDBRef::create($collection, $this->_toMongoId($contentId));
         $result = MongoDBRef::get($this->mongo, $ref);
         if ($result != null) {
             $valid = true;
             break;
         }
     }
     if (!$valid) {
         return 'Invalid content id.';
     }
     $ref = MongoDBRef::create('users', Session::getVar('_id'));
     $text = substr($this->clean(preg_replace('/\\s{6,}/', "\n\n", preg_replace('/[[:blank:]]+/', ' ', $text))), 0, 1000);
     if (empty($text)) {
         return 'Invalid comment.';
     }
     $entry = array('contentId' => (string) $contentId, 'date' => time(), 'text' => $text, 'user' => $ref, 'ghosted' => false);
     if (!$creating) {
         unset($entry['contentId'], $entry['date'], $entry['user'], $entry['ghosted']);
     }
     self::ApcPurge('getForId', $contentId);
     return $entry;
 }
All Usage Examples Of MongoDBRef::get