Wt  4.10.4
Public Member Functions | List of all members
Wt::Dbo::Dbo< C > Class Template Reference

A base class for database objects. More...

Public Member Functions

 Dbo ()
 Constructor.
 
 Dbo (const Dbo< C > &other)
 Copy constructor.
 
dbo_traits< C >::IdType id () const
 Returns the database id. More...
 
Sessionsession () const
 Returns the session. More...
 
void setDirty ()
 Marks the object as modified. More...
 
bool isDirty () const
 Returns whether this object is dirty. More...
 
ptr< C > self () const
 Returns a dbo::ptr to this object. More...
 

Detailed Description

template<class C>
class Wt::Dbo::Dbo< C >

A base class for database objects.

The only requirement for a class to be be persisted is to have a persist() method. In some cases however, it may be convenient to be able to access database information of an object, such as its database id and its session, from the object itself.

By deriving your database class directly or indirectly from this class, you can have access to its id() and session(). This will increase the size of your object with one pointer.

The following example shows a skeleton for a database object which has access to its own id and session information:

class Cat : public Wt::Dbo::Dbo<Cat> {
public:
template <class Action>
void persist(Action& a) { }
};
A base class for database objects.
Definition: ptr.h:438

Compare this to the skeleton for a minimum valid database class:

class Cat {
public:
template <class Action>
void persist(Action& a) { }
};

Member Function Documentation

◆ id()

template<class C >
dbo_traits< C >::IdType Wt::Dbo::Dbo< C >::id

Returns the database id.

Returns the database id of this object, or Wt::Dbo::dbo_traits<C>::invalidId() if the object is associated with a session or not yet stored in the database.

◆ isDirty()

template<class C >
bool Wt::Dbo::Dbo< C >::isDirty

Returns whether this object is dirty.

See also
setDirty()

◆ self()

template<class C >
ptr< C > Wt::Dbo::Dbo< C >::self

Returns a dbo::ptr to this object.

The returned pointer points to the current object only if there exists at least one other pointer to the object. Otherwise it returns a null ptr.

This means that in practice you should adopt the habit of wrapping a newly created database object directly in a ptr (and perhaps also add it to a session):

◆ session()

template<class C >
Session * Wt::Dbo::Dbo< C >::session

Returns the session.

Returns the session to which this object belongs, or nullptr if the object is not associated with a session.

◆ setDirty()

template<class C >
void Wt::Dbo::Dbo< C >::setDirty

Marks the object as modified.

When accessing a database object using ptr.modify(), the object is marked as dirty. Any intermediate query will however flush the current transaction and other changes within a member method will not be recorded.

You can call this method to achieve the same as ptr.modify() but from within member methods.