💡 Elegant and efficient way to delete and update more entities at once
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.
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
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
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.