added models used in many-to-many docs

This commit is contained in:
silverqx
2021-03-13 09:18:13 +01:00
parent 74579ce909
commit d5e8a7f0be
7 changed files with 121 additions and 6 deletions

View File

@@ -661,7 +661,7 @@ When defining the `RoleUser` model, you should extend the `Orm::Tiny::Relations:
#endif // ROLEUSER_H
You have to pass a custom pivot type to the `AllRelations` template parameter pack on `BaseModel<Model, ...AllRelations>` so that the `BaseModel` knows how to generate a `std::variant`, which holds all the relations and also add a new mapping from the relation name to the custom pivot model type-id, this is described in more detail in the [Common Rules](#common-rules):
You have to pass a custom pivot type to the `AllRelations` template parameter pack on `BaseModel<Model, ...AllRelations>` so that the `BaseModel` knows how to generate a `std::variant`, which holds all the relations and also you have to add a new mapping from the relation name to the custom pivot model type-id, this is described in more detail in the [Common Rules](#common-rules):
#ifndef ROLE_H
#define ROLE_H

View File

@@ -471,10 +471,10 @@ namespace Relations
/*! The textual representation of the Relation type. */
virtual QString relationTypeName() const = 0;
/* During eager load, we secure m_parent not to become a dangling reference in
/* During eager load, we secure m_parent to not become a dangling reference in
TinyBuilder::eagerLoadRelation() by help of the dummyModel local variable.
During lazy loads, m_parent refers to the model that initiated the lazy
load. */
It has to be the reference, because eg BelongsTo::associate() directly
modifies attributes of m_parent. */
/*! The parent model instance. */
Model &m_parent;
/*! The related model instance. */

View File

@@ -1190,9 +1190,10 @@ namespace Relations
const WithItem &relationItem)
{
/*! Helping model for eager loads, because Relation::m_parent has to be
reference, this dummy model prevents dangling reference, have to secure
reference, this dummy model prevents dangling reference, we have to secure
that the model passed to the relation method called inside getRelation()
will live long enough not to become a dangling reference.
will live long enough, to not become a dangling reference.
Have to exists, until the 'relation->match()' is processed in this method.
Look at Relation::m_parent for additional info. */
auto dummyModel = getModel().newInstance();

View File

@@ -0,0 +1,45 @@
#ifndef ROLE_H
#define ROLE_H
#include <orm/tiny/basemodel.hpp>
class User; // Forward declaration to avoid cyclic dependency
#include "models/roleuser.hpp"
using Orm::Tiny::BaseModel;
using Orm::Tiny::Relations::Pivot;
using Orm::Tiny::Relations::Relation;
class Role final : public BaseModel<Role, User, RoleUser>
//class Role final : public BaseModel<Role, User, Pivot>
{
friend BaseModel;
using BaseModel::BaseModel;
public:
/*! The users that belong to the role. */
std::unique_ptr<Relation<Role, User>>
users()
{
return belongsToMany<User>();
}
private:
/*! The visitor to obtain a type for Related template parameter. */
void relationVisitor(const QString &relation)
{
if (relation == "users")
relationVisited<User>();
else if (relation == "pivot") // Pivot
relationVisited<RoleUser>();
// relationVisited<Pivot>();
}
/*! Map of relation names to methods. */
QHash<QString, std::any> u_relations {
{"users", &Role::users},
};
};
#endif // ROLE_H

View File

@@ -0,0 +1,16 @@
#ifndef ROLEUSER_H
#define ROLEUSER_H
#include "orm/tiny/relations/basepivot.hpp"
using Orm::Tiny::Relations::BasePivot;
class RoleUser final : public BasePivot<RoleUser>
{
friend BaseModel;
friend BasePivot;
using BasePivot::BasePivot;
};
#endif // ROLEUSER_H

View File

@@ -28,6 +28,7 @@ public:
auto relation = belongsToMany<Torrent, Pivot>();
dynamic_cast<BelongsToMany<Tag, Torrent, Pivot> &>(*relation)
// .as("tagged")
.as("subscription")
.withPivot("active")
.withTimestamps();

View File

@@ -0,0 +1,52 @@
#ifndef USER_H
#define USER_H
#include <orm/tiny/basemodel.hpp>
#include "models/role.hpp"
using Orm::Tiny::BaseModel;
using Orm::Tiny::Relations::Pivot;
using Orm::Tiny::Relations::Relation;
class User final : public BaseModel<User, Role, Pivot>
{
friend BaseModel;
using BaseModel::BaseModel;
public:
/*! The roles that belong to the user. */
std::unique_ptr<Relation<User, Role>>
roles()
{
using Orm::Tiny::Relations::BelongsToMany;
// Ownership of a unique_ptr()
auto relation = belongsToMany<Role, RoleUser>();
dynamic_cast<BelongsToMany<User, Role, RoleUser> &>(*relation)
// .as("subscription")
.withPivot("active");
// .withTimestamps();
return relation;
// return belongsToMany<Role>();
// return belongsToMany<Role, RoleUser>();
}
private:
/*! The visitor to obtain a type for Related template parameter. */
void relationVisitor(const QString &relation)
{
if (relation == "roles")
relationVisited<Role>();
else if (relation == "pivot") // Pivot
relationVisited<Pivot>();
}
/*! Map of relation names to methods. */
QHash<QString, std::any> u_relations {
{"roles", &User::roles},
};
};
#endif // USER_H