Skip to content

Aggregate saves #6437

Closed
Closed
@kirkbushell

Description

@kirkbushell

This is both an "issue" and discussion piece.

It would be nice if we were able to save aggregates via their parents/children.etc when dealing with hasMany/belongsTo and other assorted relationships. Eg.

$user = new User(['email' => $email]);
$post = new Post(['title' => 'Some new post by a user']);

$user->posts()->insert($post);
$user->save();

It would be nice if this behaviour could have a waterfall effect whereby it saves the user, then the post.etc. I don't see this as particularly more complex than the current arrangement, but wondering if maybe there could be a block preventing this from happening?

In addition, it should work the other way:

$post = new Post(['title' => 'Some new post by a user']);
$user = new User(['email' => $email]);

$post->author()->associate($user);
$post->save();

This would enable us to setup all sorts of related objects and relate them before doing a final save, which would then do the trickle-down effect. It also has a rather nice side-effect of cleaning up the code.

In terms of adding this functionality, I'm wondering if, when setting up related objects - if no otherKey is available, perhaps it could defer the save until the parent/child save method is called, which could then cycle through the related objects, checking for deferred saves and save those when possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions