1. Go to this page and download the library: Download lvinkim/mongo-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/ */
lvinkim / mongo-odm example snippets
use Lvinkim\MongoODM\Annotations as ODM;
use Lvinkim\MongoODM\Entity;
use MongoDB\BSON\ObjectId;
/**
* Class User
* @ODM\Entity()
*/
class User
{
/**
* @var ObjectId
* @ODM\Id
*/
private $id;
/**
* 名称
* @var string
* @ODM\Field(type="string")
*/
private $name;
/**
* @return ObjectId
*/
public function getId(): ?ObjectId
{
return $this->id;
}
/**
* @param ObjectId $id
*/
public function setId(ObjectId $id): void
{
$this->id = $id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
}
use Lvinkim\MongoODM\Repository;
class UserRepository extends Repository
{
/**
* 返回数据库中的表名, 例如: db.user
* @return string
*/
protected function getNamespace(): string
{
return 'test.user';
}
/**
* 返回数据表的对应实体类名
* @return string
*/
protected function getEntityClassName(): string
{
return User::class;
}
}
use Lvinkim\MongoODM\DocumentManager;
use MongoDB\Driver\Manager;
$uri = 'mongodb://docker.for.mac.localhost';
$driver = new Manager($uri);
$documentManager = new DocumentManager($driver);
$userRepository = $documentManager->getRepository(UserRepository::class);
// 插入
$user = new User();
$user->setName("your name");
$userRepository->insertOne($user);
// 更多方法.... 参见 Functional/RepositoryTest.php 的各用例
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.