Update without loading

49 views
Skip to first unread message

Dylan Anderson

unread,
Jul 25, 2021, 3:08:07 PM7/25/21
to redbeanphp
I'm wondering if there's a way to update a bean without loading it.

If I have an id of album I know I can:

$myAlbum = R::load('album', $id);
$myAlbum->position = 3;
R::store($myAlbum)

But that does 1 unnecessary query.  Is there something like:

R::update('album', $id, ['position' => 3]);

I see back in 2013 the solution was to run exec() with a custom query, but I was wondering if there's been any new functionality since then.

Thanks.

robinou...@gmail.com

unread,
Jul 31, 2021, 8:28:54 AM7/31/21
to redbeanphp
Hey,

While there's not really anything to do it, if you don't want to write SQL using exec(), you could use this:
`R::getWriter()->updateRecord('album', [ [ 'property' => 'position', 'value' => 3 ] ], $id);`

It's pretty much just writing the query for you and executing it, so it still bypasses checks and FUSE events.
Not setting an id (3rd arg) inserts an new record with just the properties set in the function.
And yes, the 2nd argument is an array of arrays (ie. `[ [ 'property' => 'title', 'value' => 'Awesome album' ], [ 'property' => 'position', 'value' => 5 ] ]`

Note that this is not exposed directly in the Facade and may be changed at some point in the future, blablabla, you know the drill I suppose, to use at your own risks :D

Lyn.

robinou...@gmail.com

unread,
Jul 31, 2021, 8:30:10 AM7/31/21
to redbeanphp
And I still hate GoogleGroups for formatting (and other things but eh).

Please drop by Github if you have more questions, we're happy to help :)

Dylan Anderson

unread,
Aug 2, 2021, 11:54:06 PM8/2/21
to redbeanphp
Thanks.  I don't mind using the ORMs load() & store() if there's no shortcut methods.
Reply all
Reply to author
Forward
0 new messages