j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Save to update without query first #365

Closed lcsqlpete closed 4 years ago

lcsqlpete commented 4 years ago

Not sure if idiorm questions are still being answered but I get the impression from the documentation that in order to save an update to a row, I have to find it first. Is it possible to just save with a list of columns/values and a where to define which rows are to be updated?

treffynnon commented 4 years ago

Idiom is still maintained. Your looking for a raw query I think: https://idiorm.readthedocs.io/en/latest/querying.html#raw-queries

Would that work for you?

lcsqlpete commented 4 years ago

Thanks for the response. I took a look at the raw query and unless I'm missing something, I don't think it's possible to execute a prepared statement that way. Plus I would have to build the UPDATE statement and one of the reasons for using idiorm is to not have to construct my own SQL statements.

I think my application logic for updates is pretty common: select a specific row by primary key, send the details to the browser to load into a form so the user can make changes and submit them. When I get the changes back, I don't have the original contents of the row so I would have to read it again to use the idiorm save method. I guess I could store the original row in $_SESSION when I first find it instead. Not a show stopper, just seems a bit messy.

jspasiuk commented 4 years ago

Hi! Whats wrong about getting the record you want to update first? Most ORM included Eloquent from Laravel works that way. Maybe I'm missing something.

You get some records, send them to the front, the front let the user change the content , sends to the backend again with ids and new content. Just grab the records from the DB and update them. From the docs:

$person = ORM::for_table('person')->find_one(5); $person->set('name', 'Bob Smith'); $person->age = 20; $person->set_expr('updated', 'NOW()'); $person->save();

https://idiorm.readthedocs.io/en/latest/models.html

lcsqlpete commented 4 years ago

Nothing except that it's an extra DB access that's not needed.

treffynnon commented 4 years ago

Raw queries are prepared statements with parameter binding. Please look at the example in the docs.

lcsqlpete commented 4 years ago

Thanks, I see that now.