2017 © Pedro Peláez
 

library odm

Vegas CMF ODM

image

vegas-cmf/odm

Vegas CMF ODM

  • Friday, May 26, 2017
  • by vegas
  • Repository
  • 10 Watchers
  • 1 Stars
  • 28 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Vegas CMF ODM

Travis Status Coverage Status, (*1)

Compatible with: Phalcon >= 2.0, (*2)

Example usage

Collections definition, (*3)

namespace Fixtures\Collection;

use \Vegas\ODM\Collection;

class Category extends Collection
{
    /**
     * @var string
     */
    protected $name;

    /**
     * @var string
     */
    protected $desc;

    /**
     * @var \Fixtures\Collection\Category
     * @Mapper
     */
    protected $category;

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getDesc()
    {
        return $this->desc;
    }

    /**
     * @param mixed $desc
     */
    public function setDesc($desc)
    {
        $this->desc = $desc;
    }

    /**
     * @return Category
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * @param Category $category
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }

    public function getSource()
    {
        return 'vegas_app_categories';
    }
}

//---------------------
namespace Fixtures\Collection;

use \Vegas\ODM\Collection;

class Product extends Collection
{
    /**
     * @var string
     */
    protected $name;

    /**
     * @var \Fixtures\Collection\Category
     * @Mapper
     */
    protected $category;

    /**
     * @var int
     * @Mapper
     */
    protected $price;

    /**
     * @var \MongoDate
     * @Mapper \Vegas\ODM\Mapping\Mapper\MongoDate
     */
    protected $createdAt;

    /**
     * @var boolean
     * @Mapper
     */
    protected $isActive;

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return Category
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * @param Category $category
     */
    public function setCategory(Category $category)
    {
        $this->category = $category;
    }

    /**
     * @return int
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * @param $price
     */
    public function setPrice($price)
    {
        $this->price = $price;
    }

    /**
     * @return \MongoDate
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * @param $createdAt
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;
    }

    /**
     * @return boolean
     */
    public function isActive()
    {
        return $this->isActive;
    }

    /**
     * @param boolean $isActive
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
    }

    public function getSource()
    {
        return 'vegas_app_products';
    }
}

Working with documents, (*4)

$parentCategory = new Category();
$parentCategory->setName('Category 0');
$parentCategory->setDesc('Category 0 desc');
$parentCategory->save();

$category = new Category();
$category->setName('Category 1');
$category->setDesc('Category 1 desc');
$category->setCategory($parentCategory);
$category->save();

$product = new Product();
$product->setName('Product 1');
$product->setPrice(100);
$product->setIsActive(true);
$product->setCategory($category);
$product->setCreatedAt(time());

$product->save();

// by default Eager loading is enabled

$testProduct = Product::findFirst();
var_dump($testProduct->getCategory()->getName()); // Category 1
var_dump($testProduct->getCreatedAt()); // \MongoDate
var_dump($testProduct->getPrice()); // 100
var_dump($testProduct->getCategory()->getCategory()->getName()); // Category 0

// with disabled eager loading - efficient for big dataset

$testProduct = Product::findFirst();
var_dump($testProduct->getCategory()); // MongoId
var_dump($testProduct->getCreatedAt()); // int
var_dump($testProduct->isActive()); // true
var_dump($testProduct->getPrice()); // 100
var_dump($testProduct->getCategory()->getCategory()->getName()); // error!

Mapping cache

$config = new \Phalcon\Config([
    'mapping' => [
        'cache' => [
            'frontend' => [
                'driverClass' => 'Phalcon\Cache\Frontend\Output',
                'parameters' => [
                    'lifetime' => 3600
                ]
            ],
            'backend' => [
                'driverClass' => '\Phalcon\Cache\Backend\Mongo',
                'parameters' => [
                    'server' => 'localhost',
                    'db' => 'vegas_test',
                    'collection' => 'cache'
                ]
            ],
        ]
    ]
]);

$di->set('odmMappingCache', function() use ($di, $config) {
    $frontCacheClass = $config->mapping->cache->frontend->driverClass;
    $frontCache = new $frontCacheClass(
        $config->mapping->cache->frontend->parameters->toArray()
    );
    $backCacheClass = $config->mapping->cache->backend->driverClass;
    $cache = new $backCacheClass(
        $frontCache,
        $config->mapping->cache->backend->parameters->toArray()
    );

    return $cache;
}, true);

Mapping

Vegas ODM resolves referenced documents automatically. References must be defined in collection class by annotation. Consider the following code, (*5)

class Test extends \Vegas\ODM\Collection {
    /**
     * @var \MongoId
     * @mapper \Vegas\ODM\Mapping\Mapper\MongoId
     */
    protected $_id;

    /**
     * @var int
     * @mapper
     */
    protected $int;

    /**
     * @var \MongoDate
     * @Mapper \Vegas\ODM\Mapping\Mapper\MongoDate
     */
    protected $date;
}

Annotation @var determines the variable type. Annotation @mapper (@Mapper) determines that property value will be mapped (casted) to value defined by @var. In @mapper annotation you can specify custom mapper class. (Note! Mapping class must implements interface \Vegas\ODM\Mapping\MapperInterface), (*6)

The Versions

26/05 2017

dev-v3.0-legacy

dev-v3.0-legacy

Vegas CMF ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amsterdam Standard Vegas Team

26/05 2017

v3.0.0

3.0.0.0

Vegas CMF ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amsterdam Standard Vegas Team

19/08 2016

v3.0.x-dev

3.0.9999999.9999999-dev

Vegas CMF ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amsterdam Standard Vegas Team

23/06 2016

dev-master

9999999-dev

Vegas CMF ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amsterdam Standard Vegas Team

23/06 2016

v2.0.2-beta

2.0.2.0-beta

Vegas CMF ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amsterdam Standard Vegas Team

07/06 2016

dev-feature-proxy

dev-feature-proxy

Vegas CMF ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amsterdam Standard Vegas Team

07/06 2016

v2.0.1-beta

2.0.1.0-beta

Vegas CMF ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amsterdam Standard Vegas Team

27/04 2016

v2.0.0-beta

2.0.0.0-beta

Vegas CMF ODM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Amsterdam Standard Vegas Team