Giter VIP home page Giter VIP logo

Comments (3)

wolfy-j avatar wolfy-j commented on July 20, 2024

This is doable since the relation controls chain of persist commands, need to think about an API to avoid fetching... Let me think about it.

from orm.

alexndr-novikov avatar alexndr-novikov commented on July 20, 2024

@mrakolice technically it's possible when using Cycle, but are you sure it's a good idea to use Data mapper orm like this?
It will give too much control over persistence layer to you application layer if you modify database relations via ids
It's more usual to operate with relations as collection of objects (or inner object for HasOne case)

from orm.

mrakolice avatar mrakolice commented on July 20, 2024
$parent = [
    'field1' => 'value'
    'children' => 
    [
         ['id' => 1],
         ['id' => 2],
         ['id' => 3],
    ]

];

All child entities exists in database, but child entity with id=3 not in this particular parent.

So, when I going to do smth like this:

$parent = $orm->getRepository(Parent::class)->load('children');

I can use only entities with [1,2] id;
And this is what I can do with collection of objects:

$childIds = $parent->children->map(); // or load entities only with id via load method above

$entitiesToAdd = diff($ids, $childIds);

foreach ($orm->getRepository(Child::class)->where('id', 'in', $entitiesToAdd) as $child){
    $parent->children->add($child);
}

$transaction->persist($parent);
$transaction->run();

This approach a bit sophisticated for me and I need to fetch entities to add from database, but this is just diff operation between two arrays, no matter - in php layer or sql layer.

If I do something like this:

$entities = $orm->getRepository(Child::class)->where('id', 'in', $entitiesToAdd);
$transaction = new Transaction();

foreach ($entities as $child){
    $child->parent = $parent;
    $transaction->persist($child);
}
$transaction->run();

This will be produce many update queries on sql layer.

I can use UpdateQueryBuilder (and I've solved this problem exactly via QueryBuilder), but I think, that this problem very viable for many developers, that using Cycle ORM or any another ORM. =)

from orm.

Related Issues (20)

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.