Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

SQL Mapper: Avoid reset() after insert() or update()

70 views
Skip to first unread message

Phillip Smith

unread,
Apr 8, 2024, 1:00:33 AM4/8/24
to Fat-Free Framework
Hey folks,
Is there a way to avoid SQL Mapper objects being reset after calling insert() or update()?
The documentation says both methods should return an object, which to me implies that I could expect a mapper object with the inserted/updated record loaded. I realize that isn't explicitly stated, but that was my expectation after reading it since I'm not sure what benefit returning a dry object is.
If it matters, I'm using PostgreSQL.
Cheers,
~p

ikkez

unread,
May 27, 2024, 5:30:07 AM5/27/24
to Fat-Free Framework
It's not only resetted... actually it reloaded the object that was inserted to ensure database default value are catched. It loads the values into the current object.

Phillip Smith

unread,
May 28, 2024, 1:26:41 AM5/28/24
to ikkez via Fat-Free Framework
That's not the behavior I'm seeing on 3.8 with PostgreSQL database.

After calling insert() or update(), I'm left with a dry object and I have to explicitly load the record from the DB against using the value provided by $object->_id

Here's an example:

  $newLogin = new \LoginModel();    // Extends DB\SQL\Mapper
  $newLogin->username = 'foo';
  $newLogin->
resetVerificationState();   // Method to calculate some default values on other columns
  $newLogin->insert();

  // $newLogin is now dry; I have to load the record again manually to hydrate it with data:
  $newLogin->loadById($newLogin->_id);

I do normally have a method to override the insert() method of \DB\SQL\Mapper (which ultimately calls parent::insert() after some data sanity checks), but I removed that to test this code and I get that same same behaviour - dry() returns true after insert()

Assigning the return value of insert() to another variable (ie "$foo = $newLogin->insert()") also results in a dry mapper object.


--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Fat-Free Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/f3-framework/sVmAon475Po/unsubscribe.
To unsubscribe from this group and all its topics, send an email to f3-framework...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/f3-framework/52e8501e-2d03-494d-8134-bb7a2c0ef329n%40googlegroups.com.

ikkez

unread,
May 30, 2024, 8:57:34 AM5/30/24
to Fat-Free Framework
Is your id field a primary key?
Reply all
Reply to author
Forward
0 new messages