Wt: Wt::Dbo::Dbo< C > Class Template Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::Dbo::Dbo< C > Class Template Reference
Database Objects (Wt::Dbo)

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.
Session * | session () const
| Returns the session.
void | setDirty ()
| Marks the object as modified.
bool | isDirty () const
| Returns whether this object is dirty.
ptr< C > | self () const
| Returns a dbo::ptr to this object.

## 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) { }
};
Wt::Dbo::Dbo
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 | ( |  | ) | const

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 | ( |  | ) | const

Returns whether this object is dirty.

See also
  setDirty()

## ◆ self()

template<class C >

ptr< C > Wt::Dbo::Dbo< C >::self | ( |  | ) | const

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 | ( |  | ) | const

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.