brunohulk / yii2-mongodb-doctrine
Doctrine MongoDB ODM extension for Yii2
Installs: 2 691
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- alcaeus/mongo-php-adapter: ^1.0
- doctrine/mongodb-odm: *
- doctrine/orm: *
- mongodb/mongodb: ^1.0.0
- yiisoft/yii2: ^2.0
This package is not auto-updated.
Last update: 2024-12-12 05:58:01 UTC
README
About
This component was created with the purpose of allowing us to use all benefits of the Doctrine in Yii2 using the MongoDB ODM version. There are others solutions which use MongoDB and the active record pattern from Yii, but the vast Doctrine documentation and its better way to deal with embedded documents made its case.
Installation
Require the library with Composer:
composer require brunohulk/yii2-mongodb-doctrine
Then, to activate the component, you have to add the follow entry inside the web.php file replacing the default database params by your customise data:
'doctrineOdm' => [ 'class' => 'brunohulk\Yii2MongodbOdm\DoctrineODM', 'dsn' => 'mongodb://mongodb:27017', #DSN string connection 'dbname' => 'database_name' #Database name, 'documentsDir' => __DIR__ . '/../../documents/', # Directory which stores your mapped collections 'runtimeDir' => __DIR__ . '/../../runtime/' # The Yii2 runtime dir or other directory to store the Doctrine extra files ]
Usage
To start using the Document manager all you have to do is call the method below in any place you desire, like a controller:
class User extends Controller { private $documentManager; public function init() { $this->documentManager = Yii::$app->doctrineOdm->getDocumentManager(); } public function actionCreate() { $user = new User; $user->name = "Bruno"; $this->documentManager->persist($user); $this->documentManager->flush(); }
For the last step, is necessary to create a documents
folder within the common
directory in your Yii project, all the
documents mapped must be there, the following example is related to previous doc block.
/** * @ODM\Document(collection="user") */ class User { /** * @ODM\Id */ public $id; /** * @ODM\Field(name="name", type="string") */ public $name; }
Special thanks for David Rocha