mrsuh /
mongo odm
2.1.3
2017-07-24 08:56 UTC
Requires
- php: >=7.0
- mongodb/mongodb: ^1.1
README
简单的Mongo ODM库。
安装
将包添加到composer.json文件中的require部分。
composer require mrsuh/mongo-odm:2.*
使用方法
<?php require 'vendor/autoload.php'; use ODM\DBAL; use ODM\Document\Document; use ODM\DocumentManager\DocumentManagerFactory; /** * @ODM\Collection(name="alphabet") */ class Alphabet extends Document { /** * @ODM\Field(name="language", type="string") */ private $language; /** * @ODM\Field(name="words", type="Word[]") */ private $words; /** * Alphabet constructor. */ public function __construct() { $this->words = []; } /** * @return string */ public function getLanguage() { return $this->language; } /** * @param string $language * @return $this */ public function setLanguage(string $language) { $this->language = $language; return $this; } /** * @return Word[] */ public function getWords() { return $this->words; } /** * @param Word $word * @return $this */ public function addWord(Word $word) { $this->words[] = $word; return $this; } } class Word { /** * @ODM\Field(name="name", type="string") */ private $name; /** * @return string */ public function getName() { return $this->name; } /** * @param string $name * @return $this */ public function setName(string $name) { $this->name = $name; return $this; } } $dbal = new DBAL('127.0.0.1', 27017, 'test'); $dm_alphabet = (new DocumentManagerFactory($dbal))->init(Alphabet::class); $alphabet = new Alphabet(); $alphabet->setLanguage('English'); foreach(['a', 'b', 'c'] as $word_name) { $word = new Word(); $word->setName($word_name); $alphabet->addWord($word); } $dm_alphabet->insert($alphabet); $alphabet_from_db = $dm_alphabet->findOne(['_id' => $alphabet->getId()]); echo $alphabet_from_db->getLanguage() . ' alphabet words: ' . PHP_EOL; foreach($alphabet_from_db->getWords() as $word) { echo 'word ' . $word->getName() . PHP_EOL; }
类型
- 布尔值
- 整数
- 字符串
- 浮点数
- 数组
- 整数数组
- 字符串数组
- 浮点数数组
- \Obj
- \Obj[]