Skip to content

Commit

Permalink
Use attributes in tools/sandbox/Documents
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Feb 28, 2024
1 parent 8b83e72 commit 0d0adac
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 91 deletions.
21 changes: 7 additions & 14 deletions tools/sandbox/Documents/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,16 @@
namespace Documents;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;

/** @ODM\Document(collection="accounts") */
#[ODM\Document(collection: 'accounts')]
class Account
{
/**
* @ODM\Id
*
* @var string|null
*/
protected $id;

/**
* @ODM\Field(type="string")
*
* @var string
*/
protected $name;
#[ODM\Id]
protected ?string $id;

#[ODM\Field(type: Type::STRING)]
protected string $name;

public function __construct(string $name)
{
Expand Down
35 changes: 10 additions & 25 deletions tools/sandbox/Documents/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,22 @@
namespace Documents;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;

/** @ODM\EmbeddedDocument */
#[ODM\EmbeddedDocument]
class Address
{
/**
* @ODM\Field(type="string")
*
* @var string|null
*/
protected $street;
#[ODM\Field(type: Type::STRING)]
protected ?string $street;

/**
* @ODM\Field(type="string")
*
* @var string|null
*/
protected $city;
#[ODM\Field(type: Type::STRING)]
protected ?string $city;

/**
* @ODM\Field(type="string")
*
* @var string|null
*/
protected $state;
#[ODM\Field(type: Type::STRING)]
protected ?string $state;

/**
* @ODM\Field(type="string")
*
* @var string|null
*/
protected $postalCode;
#[ODM\Field(type: Type::STRING)]
protected ?string $postalCode;

public function getStreet(): ?string
{
Expand Down
11 changes: 4 additions & 7 deletions tools/sandbox/Documents/Phonenumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
namespace Documents;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;

/** @ODM\EmbeddedDocument */
#[ODM\EmbeddedDocument]
class Phonenumber
{
/**
* @ODM\Field(type="string")
*
* @var string|null
*/
protected $phonenumber;
#[ODM\Field(type: Type::STRING)]
protected ?string $phonenumber;

public function __construct(?string $phonenumber = null)
{
Expand Down
60 changes: 19 additions & 41 deletions tools/sandbox/Documents/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,30 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;

use function md5;

/** @ODM\Document(collection="users") */
#[ODM\Document(collection: 'users')]
class User
{
/**
* @ODM\Id
*
* @var string|null
*/
protected $id;

/**
* @ODM\Field(type="string")
*
* @var string|null
*/
private $username;

/**
* @ODM\Field(type="string")
*
* @var string|null
*/
protected $password;

/**
* @ODM\EmbedOne(targetDocument=Address::class)
*
* @var Address|null
*/
protected $address;

/**
* @ODM\ReferenceOne(targetDocument=Account::class)
*
* @var Account|null
*/
protected $account;

/**
* @ODM\EmbedMany(targetDocument=Phonenumber::class)
*
* @var Collection<int, Phonenumber>
*/
#[ODM\Id]
protected ?string $id;

#[ODM\Field(type: Type::STRING)]
private ?string $username;

#[ODM\Field(type: Type::STRING)]
protected ?string $password;

#[ODM\EmbedOne(targetDocument: Address::class)]
protected ?Address $address;

#[ODM\ReferenceOne(targetDocument: Account::class)]
protected ?Account $account;

/** @var Collection<int, Phonenumber> */
#[ODM\EmbedMany(targetDocument: Phonenumber::class)]
protected $phonenumbers;

public function __construct()
Expand Down
8 changes: 4 additions & 4 deletions tools/sandbox/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

$file = __DIR__ . '/../../vendor/autoload.php';

Expand All @@ -21,7 +21,7 @@
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setDefaultDB('doctrine_odm_sandbox');
$config->setMetadataCache(new ApcuAdapter());
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents'));
$config->setMetadataCache(new ArrayAdapter());
$config->setMetadataDriverImpl(AttributeDriver::create(__DIR__ . '/Documents'));

$dm = DocumentManager::create(null, $config);

0 comments on commit 0d0adac

Please sign in to comment.