orm icon indicating copy to clipboard operation
orm copied to clipboard

💡 Elegant and efficient way to delete and update more entities at once

Open tenmajkl opened this issue 2 years ago • 3 comments

I have an idea!

Currently, when you want to delete or update multiple items, you have to either iterate through them or work with query builder. The query builder solution works, but it doesn't have relations by default and you need to specify table name, so I think that it would be great to have something like Repository::update. The only problem that the default repository is made for Select statements so I'm not sure if that would be possible. I can add it, but I don't know the cycle codebase well at the moment.

tenmajkl avatar Nov 08 '23 20:11 tenmajkl

Hello. Please share how you imagine the API. What would the end user code look like?


Btw, you might be interested in looking at a small example of how it's possible to implement ActiveRecord based on the Cycle https://github.com/roxblnfk/cycle-active-record

roxblnfk avatar Nov 09 '23 05:11 roxblnfk

Something like

$orm->getRepository(User::class)->update([
    'name' => 'foo'
])->where('name', '!=', 'bar')->run();

or since repositories are read only:

$manager = new \Cycle\ORM\EntityManager($orm);
$manager->update(User::class)
        ->set([
            'name' => 'foo'
        ])
        ->where('name', '!=', 'bar')
        ->run()
;

But I'm not sure if this would be possible

tenmajkl avatar Nov 11 '23 19:11 tenmajkl

It looks like a request for a new interface Cycle\ORM\QueryBuilderInterface with methods select($role): Select, update(...) and delete(...)

The select() method just returns the Cycle\ORM\Select class. update() and delete() methods must provide fields mappings and relation joins like in the Select class.

roxblnfk avatar Nov 12 '23 06:11 roxblnfk