handle many-to-many relations separately when process entities
This commit is contained in:
@@ -3,10 +3,14 @@
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
|
||||
#include "matador/sql/entity.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::test {
|
||||
|
||||
struct book;
|
||||
|
||||
struct author
|
||||
{
|
||||
unsigned long id{};
|
||||
@@ -27,7 +31,7 @@ struct author
|
||||
field::attribute(op, "date_of_birth", date_of_birth, 31);
|
||||
field::attribute(op, "year_of_birth", year_of_birth);
|
||||
field::attribute(op, "distinguished", distinguished);
|
||||
field::has_many(op, "books", books, "author_id", "id", utils::fetch_type::LAZY);
|
||||
field::has_many(op, books, "author_id", utils::fetch_type::LAZY);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ struct order
|
||||
field::attribute(op, "ship_region", ship_region, 255);
|
||||
field::attribute(op, "ship_postal_code", ship_postal_code, 255);
|
||||
field::attribute(op, "ship_country", ship_country, 255);
|
||||
field::has_many(op, "order_details", order_details_, utils::fetch_type::EAGER);
|
||||
field::has_many(op, order_details_, "order_id", utils::fetch_type::EAGER);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+7
-12
@@ -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") {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user