formapro / yadm

An efficient way to treat MongoDB in PHP. Extremely fast persistence and hydration.
MIT License
85 stars 6 forks source link

Support One-to-Many Relationships with Embedded Documents. #13

Closed rubenrua closed 6 years ago

rubenrua commented 6 years ago

See the relationship pattern on Model One-to-Many Relationships with Embedded Documents from the MongoDB documentation site.

And see a implementation example using @EmbedMany with the Doctrine ODM.

makasim commented 6 years ago

That's possible

<?php
declare(strict_types=1);

namespace App\Model;

use function Makasim\Values\add_object;
use function Makasim\Values\get_object;
use function Makasim\Values\get_objects;
use function Makasim\Values\set_object;

class Campaign
{
    private $values = [];
    private $objects = [];

    public function setState(CampaignState $state): void
    {
        set_object($this,'state', $state);
        add_object($this, 'previousStates', $state);
    }

    public function getState(): CampaignState
    {
        return get_object($this, 'state');
    }

    public function getPreviousStates(): array
    {
        return get_objects($this, 'previousStates');
    }
}
rubenrua commented 6 years ago

Ohhh, thank you.

I will try it.

rubenrua commented 6 years ago

My test:

<?php

declare(strict_types=1);

require_once __DIR__.'/vendor/autoload.php';

use MongoDB\Client;
use Makasim\Yadm\Hydrator;
use Makasim\Yadm\Storage;
use function Makasim\Values\add_object;
use function Makasim\Values\get_object;
use function Makasim\Values\get_objects;
use function Makasim\Values\set_object;
use function Makasim\Values\get_value;
use function Makasim\Values\set_value;
use function Makasim\Yadm\get_object_id;

class CampaignState
{
    private $values = [];
    private $objects = [];

    public function setTitle(string $title): void
    {
        set_value($this, 'title', $title);
    }

    public function getTitle(): string
    {
        return get_value($this, 'title');
    }
}

class Campaign
{
    private static $i = 0;

    private $values = [];
    private $objects = [];

    public function setState(CampaignState $state): void
    {
        set_value($this, 'i', self::$i);
        set_object($this, 'state', $state);
        add_object($this, 'previousStates', $state);
    }

    public function getState(): CampaignState
    {
        return get_object($this, 'state');
    }

    public function getPreviousStates(): array
    {
        return iterator_to_array(get_objects($this, 'previousStates', CampaignState::class));
    }
}

$collection = (new Client())->selectCollection('acme_demo', 'orders');
$hydrator = new Hydrator(Campaign::class);
$storage = new Storage($collection, $hydrator);

$campaign = new Campaign();
$state = new CampaignState();
$state->setTitle('1.- 123');
$campaign->setState($state);
$state2 = new CampaignState();
$state2->setTitle('2.- 123');
$campaign->setState($state2);

$storage->insert($campaign);

$foundCampaign = $storage->findOne(['_id' => get_object_id($campaign)]);
var_dump($foundCampaign);
var_dump($foundCampaign->getPreviousStates());
makasim commented 6 years ago

@rubenrua Working?

rubenrua commented 6 years ago

Yes.