[odb-users] Query to many relation
Florent GRATTA
gratta at visuol.com
Thu Jul 9 10:25:02 EDT 2015
Hi,
I have the following data model
#pragma db object
class UserProfile
{
public:
UserProfile()
{
}
const std::string& getEmail() const
{
return m_email;
}
void setEmail(const std::string& email)
{
m_email = email;
}
const std::string& getFirstName() const
{
return m_firstName;
}
void setFirstName(const std::string& firstName)
{
m_firstName = firstName;
}
const std::string& getLastName() const
{
return m_lastName;
}
void setLastName(const std::string& lastName)
{
m_lastName = lastName;
}
const std::string& getPassword() const
{
return m_password;
}
void setPassword(const std::string& password)
{
m_password = password;
}
const std::string& getLogin() const
{
return m_login;
}
void setLogin(const std::string& login)
{
m_login = login;
}
private:
#pragma db id
std::string m_login;
std::string m_password;
std::string m_firstName;
std::string m_lastName;
std::string m_email;
};
---------------------------------------------------------------
#pragma db object
class UserGroup
{
public:
UserGroup()
{
}
const std::string& getName() const
{
return m_name;
}
void setName(const std::string& name)
{
m_name = name;
}
const std::vector<std::shared_ptr<UserProfile> >& getUserprofiles()
const
{
return m_userprofiles;
}
void setUserprofiles(
const std::vector<std::shared_ptr<UserProfile> >& userprofiles)
{
m_userprofiles = userprofiles;
}
private:
#pragma db id
std::string m_name;
#pragma db value_not_null unordered
std::vector<std::shared_ptr<UserProfile>> m_userprofiles;
};
---------------------------------------------------------------
I would like to return a list of UserGroup associated to the
UserProfile. But compiler tolds me that:
error: 'm_userprofiles' is not a member of 'query {aka
odb::query<UserGroup>}' at line result r (m_db->query<UserGroup>
(query::m_userprofiles->login == login));
My code :
typedef odb::query<UserGroup> query;
typedef odb::result<UserGroup> result;
std::shared_ptr<std::list<UserGroup>> listUserGroups (new
std::list<UserGroup>());
try
{
transaction t (m_db->begin ());
result r (m_db->query<UserGroup> (query::m_userprofiles->login ==
login));
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
listUserGroups ->push_back(*i);
}
t.commit ();
}
catch (const odb::exception& e)
{
std::wcout << e.what () << std::endl;
//return nullptr;
}
return listUserGroups ;
More information about the odb-users
mailing list