handle many-to-many relations separately when process entities
This commit is contained in:
+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