handle many-to-many relations separately when process entities

This commit is contained in:
2024-04-07 14:45:57 +02:00
parent 30e2e0c221
commit fa7ff832c9
19 changed files with 484 additions and 96 deletions
+7 -12
View File
@@ -6,6 +6,7 @@
#include "matador/utils/foreign_attributes.hpp"
#include "matador/sql/entity.hpp"
#include "matador/sql/has_many_to_many_relation.hpp"
#include <string>
@@ -23,7 +24,7 @@ struct ingredient
namespace field = matador::utils::access;
field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255);
field::has_many(op, "recipes", recipes, "ingredient_id", "recipe_id", utils::fetch_type::EAGER);
field::has_many_to_many(op, "recipe_ingredients", recipes, "ingredient_id", "recipe_id", utils::fetch_type::EAGER);
}
};
@@ -38,21 +39,15 @@ struct recipe
namespace field = matador::utils::access;
field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255);
field::has_many(op, "ingredients", ingredients, "recipe_id", "ingredient_id", utils::fetch_type::EAGER);
field::has_many_to_many(op, "recipe_ingredients", ingredients, utils::fetch_type::LAZY);
}
};
struct recipe_ingredient
class recipe_ingredient : public sql::has_many_to_many_relation<recipe, ingredient>
{
sql::entity<recipe> recipe_;
sql::entity<ingredient> ingredient_;
template<class Operator>
void process(Operator &op) {
namespace field = matador::utils::access;
field::belongs_to(op, "recipe_id", recipe_, utils::default_foreign_attributes);
field::belongs_to(op, "ingredient_id", ingredient_, utils::default_foreign_attributes);
}
public:
recipe_ingredient()
: has_many_to_many_relation("recipe_id", "ingredient_id") {}
};
}