Giter Club home page Giter Club logo

Comments (5)

makasim avatar makasim commented on July 28, 2024

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');
    }
}

from yadm.

rubenrua avatar rubenrua commented on July 28, 2024

Ohhh, thank you.

I will try it.

from yadm.

rubenrua avatar rubenrua commented on July 28, 2024

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());

from yadm.

makasim avatar makasim commented on July 28, 2024

@rubenrua Working?

from yadm.

rubenrua avatar rubenrua commented on July 28, 2024

Yes.

from yadm.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.