Project

General

Profile

How to query many to many relationship?

Added by Eli Vatsaas 9 months ago

I have an employee class, declared here:
class Employee {
public:
std::string firstName;
std::string lastName;
std::string email;
std::string phoneNumber;
Wt::WDate hireDate;
Wt::WDate birthDate;
int payRate;
dbo::collection< dbo::ptr > employeeRoles;
dbo::collection< dbo::ptr > employeeStores;
dbo::collection< dbo::ptr > availabilities;
dbo::collection< dbo::ptr > shiftHours;
dbo::collection< dbo::ptr > shifts;

template
void persist(Action& a)
{
dbo::field(a, firstName, "FirstName");
dbo::field(a, lastName, "LastName");
dbo::field(a, email, "Email");
dbo::field(a, phoneNumber, "PhoneNumber");
dbo::field(a, hireDate, "HireDate");
dbo::field(a, birthDate, "BirthDate");
dbo::field(a, payRate, "PayRate");
dbo::hasMany(a, employeeRoles, dbo::ManyToMany, "EmployeeRoles");
dbo::hasMany(a, employeeStores, dbo::ManyToMany, "EmployeeStores");
dbo::hasMany(a, availabilities, dbo::ManyToOne, "Employee");
dbo::hasMany(a, shiftHours, dbo::ManyToOne, "Employee");
dbo::hasMany(a, shifts, dbo::ManyToOne, "Employee");
}
};

and a Role class, declared here:
class Role {
public:
std::string roleName;
dbo::collection< dbo::ptr > employees;
dbo::collection< dbo::ptr > shifts;

    template<class Action>
    void persist(Action& a)
    {
            dbo::field(a, roleName,     "RoleName");
            dbo::hasMany(a, employees, dbo::ManyToMany, "EmployeeRoles");
             dbo::hasMany(a, shifts, dbo::ManyToOne, "Role");
    }
 };

I need to be able to retrieve employees that have a specific role. This could easily be done with a sql query, but I haven't been able to get any sql queries to work. I get an error parsing the query. Does anyone have suggestions for accessing this information from the Employee class? I can't find any examples that retrieve data from these tables.

My current attempt :
Wt::Dbo::QueryWt::Dbo::ptr> query{session.find()};
query = query.where("employeeStores = ?");
query = query.bind("Downtown");
Wt::Dbo::collection
Wt::Dbo::ptr> collection{query.resultList()};
vector
Wt::Dbo::ptr> resultsVec{collection.begin(), collection.end()};
creates an error due to employeStores being an unknown column.


Replies (1)

RE: How to query many to many relationship? - Added by Eli Vatsaas 9 months ago

Got it working. Could still use help getting my sql stored procedures, or statements in general, using session.query() to work.

    (1-1/1)