Code Monkey home page Code Monkey logo

mongo-odm's Introduction

mongo odm

Latest Stable Version Total Downloads License

Simple Mongo ODM library.

Installation

Add package to your require section in the composer.json file.

composer require mrsuh/mongo-odm:2.*

Usage

<?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;
}

Types

  • bool
  • integer
  • string
  • float
  • array
  • integer[]
  • string[]
  • float[]
  • \Obj
  • \Obj[]

mongo-odm's People

Contributors

mrsuh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.