1. Go to this page and download the library: Download jpc/mongodb-odm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jpc / mongodb-odm example snippets
namespace ACME\Model;
class MyDoc {
private $id;
private $field1
private $field2;
//GETTERS AND SETTERS
}
namespace ACME\Model;
use JPC\MongoDB\ODM\Annotations\Mapping as ODM;
/**
* @ODM\Document("my_collection")
*/
class MyDoc {
/**
* @ODM\Id
*/
private $id;
/**
* @ODM\Field('my_first_field')
*/
private $field1
/**
* @ODM\Field('my_second_field')
*/
private $field2;
//GETTERS AND SETTERS
}
namespace ACME\Model;
use JPC\MongoDB\ODM\Annotations\Mapping as ODM;
class MyEmbedded {
/**
* @ODM\Field('one_field_emb')
*/
private $oneFieldEmb
/**
* @ODM\Field('two_field_emb')
*/
private $twoFieldEmb;
//GETTERS AND SETTERS
}
use JPC\MongoDB\ODM\Factory\DocumentManagerFactory;
$factory = new DocumentManagerFactory();
$factory->createDocumentManager("mongodb://user:[email protected]:27017/authdb", "my_db");
public function getRepository($modelName, $collection = null)
public function find($id, $projections = [], $options = []);
public function findAll($projections = [], $sorts = [], $options = []);
public function findBy($filter, $projections = [], $sorts = [], $options = []);
public function findOneBy($filter = [], $projections = [], $sorts = [], $options = []);
public function findAndModifyOneBy($filter = [], $update = [], $projections = [], $sorts = [], $options = []);
[...]
$repository = $documentManager->getRepository("ACME\Model\MyDoc");
// NOTE: filter, projections, etc... can be property name or field name
$myDocument = $repository->findOneBy(["field1" => "my_field_value", "myEmbedded.oneFieldEmb" => "another_value"]);
$obj = new MyDoc();
$obj->setField1("MyValue");
$obj->setField2("MySecondValue");
public function persist($object, $collection = null);
foreach($docs as $index => $doc){
$doc->setField2($index);
}
$documentManager->flush();
foreach($docs as $doc){
$documentManager->remove($doc);
}
$documentManager->flush();
$documentManager->unpersist($obj);
$documentManager->clear();
namespace ACME\Model;
use JPC\MongoDB\ODM\Annotations\Mapping as ODM;
use JPC\MongoDB\ODM\GridFS\Annotations\Mapping as GFS;
use JPC\MongoDB\ODM\GridFS\Document;
/**
* @GFS\Document("my_gridfs_bucket")
*/
class MyGridFSDoc extends Document {
/**
* @ODM\Field('my_meta_1')
* @GFS\Metadata
*/
private $meta1;
}
namespace ACME\Model;
use JPC\MongoDB\ODM\Annotations\Mapping as ODM;
use JPC\MongoDB\ODM\Annotations\Event;
/**
* @ODM\Document("my_collection")
* @Event\HasLifecycleCallbacks
*/
class MyDoc {
//...
}
namespace ACME\Model;
use JPC\MongoDB\ODM\Annotations\Mapping as ODM;
use JPC\MongoDB\ODM\Annotations\Event;
/**
* @ODM\Document("my_collection")
* @Event\HasLifecycleCallbacks
*/
class MyDoc {
//...
/**
* @Event\PreFlush
*/
public function updateTimestamp(){
$this->date = time();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.