1. Go to this page and download the library: Download vegas-cmf/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/ */
vegas-cmf / odm example snippets
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';
}
}
$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!