Dbo Modify modifying the object when nothing changes
Added by Mark Petryk almost 8 years ago
Koen, you and I developed this chunk of code together, and I've got a symptom with it that I don't like and want to change... and knowing you, it'll probably take you about two seconds to explain it;
15 template <class C>·
16 class BaseDboFormModel·
17 : public BaseFormModel·
18 {·
19 ·
20 public:·
21 ·
22 BaseDboFormModel·
23 (·
24 int id,·
25 Wt::Dbo::Session * session,·
26 Wt::WObject * parent = 0·
27 )·
28 : BaseFormModel( C::getFieldDefs(), parent ),·
29 m_session(session)·
30 {·
31 if( id == -1 )·
32 m_item = Wt::Dbo::ptr<C>( new C() );·
33 else·
34 {·
35 Wt::Dbo::Transaction t(*m_session);·
36 m_item = m_session-> load<C>(id);·
37 }·
38 ·
39 std::cout << __FILE__ << " " << __LINE__ << " " << m_item.id() << " " << m_item.version() << std::endl;·
40 ·
41 read();·
42 ·
43 std::cout << __FILE__ << " " << __LINE__ << " " << m_item.id() << " " << m_item.version() << std::endl;·
44 }·
45 ·
46 void read()·
47 {·
48 isReadMode = true;·
49 ·
50 std::cout << __FILE__ << " " << __LINE__ << " " << m_item.id() << " " << m_item.version() << std::endl;·
51 ·
52 {·
53 Wt::Dbo::Transaction t(*m_session);·
54 m_item.modify()-> persist(*this);· <--------- this is changing the item version
55 }·
56 ·
57 std::cout << __FILE__ << " " << __LINE__ << " " << m_item.id() << " " << m_item.version() << std::endl;·
58 ·
59 m_item.modify()-> postRead();·
60 ·
61 std::cout << __FILE__ << " " << __LINE__ << " " << m_item.id() << " " << m_item.version() << std::endl;·
62 }·
This code produces the following output;
./src/dbo/dbobasedboformmodel.h 39 8 107
./src/dbo/dbobasedboformmodel.h 50 8 107
update "mktCampaign" set "version" = $1, "isActive" = $2, "isVisible" = $3,...
./src/dbo/dbobasedboformmodel.h 57 8 108
./src/dbo/dbobasedboformmodel.h 61 8 108
./src/dbo/dbobasedboformmodel.h 43 8 108
Basically, all I'm trying to do is get this Dbo object loaded from the database. It works great, except for the fact that the 'persist()' function call on line 54 is causing the item version to be changed. I don't want this, because it's unnecessary. The object really isn't dirty, it's just been loaded from the back-end database, that's all. All I'm using the persist function for is to gain access to the fields and the data in them. And, then, of course, on item-save I do actually write out the data, but on this one I'm just reading.
How can I call a 'const' version of the persist() function here so that the version() number of the item doesn't get bumped on a simple read? Or is there something in the library that could be changed?
~mark
http://www.lorimarksolutions.com
check out our Wt site at;
http://www.creditcardbuilders.com
Replies (4)
RE: Dbo Modify modifying the object when nothing changes - Added by Koen Deforche almost 8 years ago
Mark, if you're sure that you're not modifying the object (and thus do not need a SQL Update for it), you can const_cast<> the result of .get() instead of calling .modify() ?
RE: Dbo Modify modifying the object when nothing changes - Added by Mark Petryk almost 8 years ago
I'm just trying to call persist() so I can fetch all the fields out in to my model. Here's the entire object;
15 template <class C>·
16 class BaseDboFormModel·
17 : public BaseFormModel·
18 {·
19 ·
20 public:·
21 ·
22 BaseDboFormModel·
23 (·
24 int id,·
25 Wt::Dbo::Session * session,·
26 Wt::WObject * parent = 0·
27 )·
28 : BaseFormModel( C::getFieldDefs(), parent ),·
29 m_session(session)·
30 {·
31 if( id == -1 )·
32 m_item = Wt::Dbo::ptr<C>( new C() );·
33 else·
34 {·
35 Wt::Dbo::Transaction t(*m_session);·
36 m_item = m_session-> load<C>(id);·
37 }·
38 ·
39 read();·
40 }·
41 ·
42 void read()·
43 {·
44 isReadMode = true;·
45 Wt::Dbo::Transaction t(*m_session);·
46 m_item.modify()-> persist(*this);·
47 m_item.modify()-> postRead();·
48 }·
49 ·
50 int accept()·
51 {·
52 {·
53 isReadMode = false;·
54 Wt::Dbo::Transaction t(*m_session);·
55 ·
56 if( m_item.id() == -1 )·
57 m_session-> add( m_item );·
58 ·
59 m_item.modify()-> persist(*this);·
60 m_item.modify()-> preWrite();·
61 }·
62 ·
63 {·
64 Wt::Dbo::Transaction t(*m_session);·
65 m_item.reread();·
66 }·
67 ·
68 read();·
69 ·
70 return m_item.id();·
71 }·
72 ·
73 Field field( const std::string & fieldName )·
74 {·
75 for( unsigned int i=0; i< fields().size(); i++ )·
76 if( fields()[i] == fieldName )·
77 return fields()[i];·
78 ·
79 return 0;·
80 }·
81 ·
82 template <typename T>·
83 void act( Wt::Dbo::FieldRef<T> fieldRef )·
84 {·
85 if( isReadMode )·
86 {·
87 setValue·
88 (·
89 field(fieldRef.name()),·
90 boost::any(fieldRef.value())·
91 );·
92 }·
93 else·
94 {·
95 fieldRef.setValue·
96 (·
97 boost::any_cast<T>·
98 (·
99 Wt::convertAnyToAny·
100 (·
101 value·
102 (·
103 field·
104 (·
105 fieldRef.name()·
106 )·
107 ),·
108 typeid(T)·
109 )·
110 )·
111 );·
112 }·
122 ·
123 }·
124 ·
125 template <typename T>·
126 void actCollection( const Wt::Dbo::CollectionRef<T> & /* collectionRef */ )·
127 {·
128 #ifdef NEVER·
129 std::cout << __FILE__ << " " << __LINE__ << " "·
130 << collectionRef.joinName().c_str()·
131 << std::endl·
132 ;·
133 #endif·
134 }·
135 ·
136 template <typename T>·
137 void actPtr( const Wt::Dbo::PtrRef<T> & /* ptrRef */ )·
138 {·
139 #ifdef NEVER·
140 std::cout << __FILE__ << " " << __LINE__ << " "·
141 << ptrRef.name().c_str()·
142 << std::endl·
143 ;·
144 #endif·
145 }·
146 ·
147 Wt::Dbo::Session * session() const·
148 {·
149 return m_session;·
150 }·
151 ·
152 protected:·
153 ·
154 /*·
155 ** maintain a pointer to the specific item we are editing.·
156 **·
157 */·
158 Wt::Dbo::ptr<C> m_item;·
159 ·
160 private:·
161 ·
162 /*·
163 ** This model is either reading or writing. default·
164 ** to not writing (therefore reading).·
165 **·
166 */·
167 bool isReadMode = true;·
168 ·
169 /*·
170 ** session variable·
171 **·
172 */·
173 Wt::Dbo::Session * m_session;·
174 ·
175 }; // endtemplate <class C> class BaseDboFormModel·
176 ·
179 ·
RE: Dbo Modify modifying the object when nothing changes - Added by Mark Petryk almost 8 years ago
That works perfectly, Koen! Thank you, sir.
(see... I knew it would take you about two seconds)
RE: Dbo Modify modifying the object when nothing changes - Added by Mark Petryk almost 8 years ago
Here is the modified code segment;
42 void read()·
43 {·
44 isReadMode = true;·
45 Wt::Dbo::Transaction t(*m_session);·
46 const_cast<C*>(m_item.get())-> persist(*this);·
47 const_cast<C*>(m_item.get())-> postRead();·
48 }·
49 ·