com D de Mongo
PHP Community Summit 2018
São Paulo - SP
Biblioteca PHP com serviços de persistência e mapeamento
www.doctrine-project.org
Object Relational Mapper
Object Document Mapper
composer require alcaeus/mongo-php-adapter
composer require doctrine/mongodb-odm
/bin/console.php
/bin/console.php
<?php
namespace Application\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
// use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @Document(collection="user", repositoryClass="")
*/
class User
{
}
/**
* @var string
* @Id(name="id", type="string")
*/
private $id;
/**
* @var string
* @Field(name="name", type="string")
*/
private $name;
/**
* @var string
* @Field(name="email", type="string") @UniqueIndex()
*/
private $email;
/**
* @var array
* @Field(type="collection")
*/
private $roles = [];
/**
* @var Address
* @ODM\Raw()
* @Field(type="raw")
*/
private $address;
use Doctrine\ODM\MongoDB\Mapping\Annotations\EmbeddedDocument;
/**
* Class Address
* @package Application\Document
*
* @EmbeddedDocument()
*/
class Address
{
}
/**
* @var Address
* @ODM\EmbedOne(targetDocument="Address")
*/
private $address;
php bin/console.php odm:schema:create
by LibSodium
/**
* @var \DateTime
* @Field(type="date", nullable=true)
*/
private $registered;
/**
* @var \DateTime
* @Field(type="date", nullable=true)
*/
private $updated;
/**
* @ODM\PrePersist()
*/
public function prePersist()
{
$this->registered = new \DateTime();
}
/**
* @ODM\PreUpdate()
*/
public function preUpdate()
{
$this->updated = new \DateTime();
}
namespace Application\Repository;
use Application\Document\User;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentRepository;
class UserRepository extends DocumentRepository
{
public function __construct(DocumentManager $documentManager)
{
parent::__construct(
$documentManager,
$documentManager->getUnitOfWork(),
$documentManager->getClassMetadata(User::class)
);
}
}
public function find($id, $lockMode = LockMode::NONE, $lockVersion = null)
{
return parent::find($id, $lockMode, $lockVersion);
}
public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null)
{
return parent::findBy($criteria, $sort, $limit, $skip);
}
public function findAll()
{
return parent::findAll();
}
public function findOneBy(array $criteria)
{
return parent::findOneBy($criteria);
}
public function insert(User $user)
{
$this->getDocumentManager()->persist($user);
}
public function deleteOne(User $user)
{
$this->getDocumentManager()->remove($user);
}
public function findWithProjection(array $fields)
{
$queryBuilder = $this->getDocumentManager()->createQueryBuilder();
return $queryBuilder
->select($fields) // ['name', 'email']
->getQuery()->execute();
}
public function findUserByPeriodRegister($initial, $final)
{
$builder = $this->createAggregationBuilder();
$builder
->match()
->field('registered')
->gte($initial)
->lt($final)
->field('status')
->equals(true);
return $builder->execute();
}
www.doctrine-project.org/projects/doctrine-mongodb-odm/en/1.2/
Q faço eu da vida sem vc?
Valeu Vaqueiro.
@feitozaAle
@alessandrofeitoza
https://github.com/alessandrofeitoza/doctrine-odm-example