Project

General

Profile

Issue with deleting objects through .remove() function Wt::Dbo

Added by Rajveer Shringi about 6 years ago

Hello

I am trying to remove a user object stored in MySql using .remove() function as pointed in the tutorial -

@

dbo::Transaction transaction(session_);

const Wt::Auth::User& u = (session_).login().user();

Wt::Dbo::ptr user = (session_).user(u);

user.remove();@

And the error I am receiving is -

error: passing 'const Wt::Dbo::ptr' as 'this' argument discards qualifiers [-fpermissive]

user.remove();

.

.

.

/usr/include/Wt/Dbo/ptr_impl.h:505:6: note: in call to 'void Wt::Dbo::ptr::remove() [with C = User]'

void ptr::remove()

Can someone please point what I might be doing wrong.


Replies (7)

RE: Issue with deleting objects through .remove() function Wt::Dbo - Added by lm at about 6 years ago

I just did the same thing and I don't get that error.

 52 user_ptr get_logged_in_user(void)                                                                
 53 {                                                                                                                 
 54     if (!login.loggedIn())                                                                                        
 55         return nullptr;                                                                                           
 56     Wt::Dbo::Transaction t{session};                                                                              
 57     auto auth_info{users.find(login.user())};                                                                     
 58     auto ret{auth_info->user()};                                                                                  
 59     ret.remove();                                                                                                 
 61     return ret;                                                                                                   
 62 }     

Assuming you're using the example I think you are, this code should be roughly equivalent. It seems like something else is going on...can you show more code maybe? I know a minimum working example is a lot to ask in this case! But if your code is on github...

RE: Issue with deleting objects through .remove() function Wt::Dbo - Added by Mark Petryk about 6 years ago

Rajveer, can you paste the function definition that this is being run in?

For example, in the post above, the function definition is:

user_ptr get_logged_in_user(void)

What is yours?

RE: Issue with deleting objects through .remove() function Wt::Dbo - Added by Rajveer Shringi about 6 years ago

Hey Mark, lm

Thank you for responding. The above example now works, what I realized is I was loosing scope of the transaction i.e. from the point where I start my dbo transaction and the line where I actually call the .remove() method, some nesting already took place and "maybe" that was what that caused this error. It works now since I changed the control flow.

My function definition is a simple container widget that is populating some table from back-end(MySQL) and on clicking on certain button the specific user gets deleted(vanilla CRUD functionality).

Wt::WContainerWidget *UserScreen::populateListModelsTable()

Although I have another few things that I can use some clarity on-

1. How can I access auto generated object field "id" when my objects are stored in MySQL. On calling a usual object->id or object.id an error is thrown saying the stated class does not have a member variable called "id"(where object is a Wt::Dbo::ptr).

2. For a scenario where user object can have multiple filling model object on trying to iterate over the filling model objects via the following-

for (dbo::collection< dbo::ptr<FillingModel> >::const_iterator i =
user->fillingModel.begin(); i != user->fillingModel.end(); ++i) {
const FillingModel& fm = **i;}


I can access specific attribute values for each filling model via fm.attribute_name, but I can not access fm.id (same error as mentioned in 1. above). Also I can not use fm.remove() on this either.

As a workaround I am initializing another dbo::ptr and then call .remove() method on this. It works but I am not happy with this solution as I am not able to understand what is going wrong here.

dbo::ptr<FillingModel> fmodel = session_.find<FillingModel>().where("shortModelDesc = ?").bind(fm.shortModelDesc);

Thanks again for participating in this discussion.

RV

RE: Issue with deleting objects through .remove() function Wt::Dbo - Added by Mark Petryk about 6 years ago

Ok, good! I realized my question made no sense... so good, glad you found that issue;

1. How can I access auto generated object field "id"

(psudo code)

node: resultValue() returns 1 item, and throws on multiple

   auto myItem = session()-> find<MyClass>().where("id = ?").bind( myId ).resultValue();
   std::cout << myItem.id() << std::endl;

...or something to that effect.

2. For a scenario where user object can have multiple filling model object

note: resultList() returns an item collection

   auto myItems = session()-> find<MyClass>().where("city = ?").bind( mycity ).resultList();
   for( auto item : myItems )
   {
     cout << item.id() << " " << item-> someField() << endl;
   }

...or something to that effect

RE: Issue with deleting objects through .remove() function Wt::Dbo - Added by lm at about 6 years ago

Rajveer Shringi wrote:

  1. How can I access auto generated object field "id" when my objects are stored in MySQL. On calling a usual object->id or object.id an error is thrown saying the stated class does not have a member variable called "id"(where object is a Wt::Dbo::ptr).

Make sure your class extends Wt::Dbo::Dbo. This base class will provide some convenience: an id(), ready access to the Wt::Dbo::Session, and a Wt::Dbo::ptr to this. https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Dbo.html

RE: Issue with deleting objects through .remove() function Wt::Dbo - Added by Rajveer Shringi about 6 years ago

lm at wrote:

Make sure your class extends Wt::Dbo::Dbo. This base class will provide some convenience: an id(), ready access to the Wt::Dbo::Session, and a Wt::Dbo::ptr to this. https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Dbo.html

Thanks, this works. I was not extending my object classes from this base class.

RE: Issue with deleting objects through .remove() function Wt::Dbo - Added by lm at about 6 years ago

Oh yeah, there is a huge caveat (I think it's still applicable): delete your copy constructor (or at least don't use it). I remember running into some terribly confusing bugs that Mr. Koen helped me work through and the answer was: Wt::Dbo::Dbo doesn't support copy (but doesn't delete its copy constructor either).

That was a long time ago, and I'm hopeful the issue has been more satisfactorily dispatched in the meantime, but I haven't bothered testing a recent version.

    (1-7/7)