-
-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
202 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\Introduction; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
#[ODM\EmbeddedDocument] | ||
class Address | ||
{ | ||
public function __construct( | ||
#[ODM\Field] | ||
public string $address, | ||
#[ODM\Field] | ||
public string $city, | ||
#[ODM\Field] | ||
public string $state, | ||
#[ODM\Field] | ||
public string $zipcode, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\Introduction; | ||
|
||
use DateTimeImmutable; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
#[ODM\MappedSuperclass] | ||
abstract class BaseEmployee | ||
{ | ||
#[ODM\Field(strategy: 'increment')] | ||
public int $changes = 0; | ||
|
||
/** @var string[] */ | ||
#[ODM\Field] | ||
public array $notes = []; | ||
|
||
#[ODM\Field] | ||
public string $name; | ||
|
||
#[ODM\Field] | ||
public int $salary; | ||
|
||
#[ODM\Field] | ||
public DateTimeImmutable $started; | ||
|
||
#[ODM\Field] | ||
public DateTimeImmutable $left; | ||
|
||
#[ODM\EmbedOne] | ||
public Address $address; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\Introduction; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
#[ODM\Document] | ||
class Employee extends BaseEmployee | ||
{ | ||
#[ODM\Id] | ||
public readonly string $id; | ||
|
||
#[ODM\ReferenceOne(targetDocument: Manager::class)] | ||
public ?Manager $manager = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\Introduction; | ||
|
||
use DateTimeImmutable; | ||
use Doctrine\ODM\MongoDB\Tests\BaseTestCase; | ||
|
||
class IntroductionTest extends BaseTestCase | ||
{ | ||
public function testIntroduction(): void | ||
{ | ||
// Insert | ||
$employee = new Employee(); | ||
$employee->name = 'Employee'; | ||
$employee->salary = 50000; | ||
$employee->started = new DateTimeImmutable(); | ||
$employee->address = new Address( | ||
address: '555 Doctrine Rd.', | ||
city: 'Nashville', | ||
state: 'TN', | ||
zipcode: '37209', | ||
); | ||
|
||
$project = new Project('New Project'); | ||
$manager = new Manager(); | ||
$manager->name = 'Manager'; | ||
$manager->salary = 100_000; | ||
$manager->started = new DateTimeImmutable(); | ||
$manager->projects->add($project); | ||
|
||
$this->dm->persist($employee); | ||
$this->dm->persist($project); | ||
$this->dm->persist($manager); | ||
$this->dm->flush(); | ||
$this->dm->clear(); | ||
|
||
$employee = $this->dm->find(Employee::class, $employee->id); | ||
$this->assertInstanceOf(Address::class, $employee->address); | ||
|
||
$manager = $this->dm->find(Manager::class, $manager->id); | ||
$this->assertInstanceOf(Project::class, $manager->projects[0]); | ||
|
||
// Update | ||
$newProject = new Project('Another Project'); | ||
$manager->salary = 200_000; | ||
$manager->notes[] = 'Gave user 100k a year raise'; | ||
$manager->changes++; | ||
$manager->projects->add($newProject); | ||
|
||
$this->dm->persist($newProject); | ||
$this->dm->flush(); | ||
$this->dm->clear(); | ||
|
||
$manager = $this->dm->find(Manager::class, $manager->id); | ||
$this->assertSame(200_000, $manager->salary); | ||
$this->assertCount(1, $manager->notes); | ||
$this->assertSame(1, $manager->changes); | ||
$this->assertCount(2, $manager->projects); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\Introduction; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
#[ODM\Document] | ||
class Manager extends BaseEmployee | ||
{ | ||
#[ODM\Id] | ||
public string $id; | ||
|
||
/** @var Collection<Project> */ | ||
#[ODM\ReferenceMany(targetDocument: Project::class)] | ||
public Collection $projects; | ||
|
||
public function __construct() | ||
{ | ||
$this->projects = new ArrayCollection(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\Introduction; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
#[ODM\Document] | ||
class Project | ||
{ | ||
#[ODM\Id] | ||
public string $id; | ||
|
||
public function __construct( | ||
#[ODM\Field] | ||
public string $name, | ||
) { | ||
} | ||
} |