@hidechron
I’m not sure what you want, but there’s two possible answers:
If you only want one copy of each game per user, but want to assign different categories based on the user:
You can use Join Fields to store the category on the join itself. For example, add a category_id column to the join table, then you can include use column in your queries. It’s not very elegant.
If you are trying to allow a user to have several copies of the same game stored in different categories:
DMZ only supports one relationship between two objects. The solution is to create an interim model that handles the more complex connections:
User has many User_categories
User_category has one User
User_category has one Category
Category has many User_categories
User_category has many Games
Game has many User_categories
User_category is simply an object to handle the connection.
Then you can use Deep Relationships to build your query, like so:
// list games for this user and category
$games = new Game();
$games->where_related('user_category/category', 'id', $_POST['category_id']);
$games->where_related('user_category/user', $logged_in_user);
// add in sorting, additional parameters, etc.
$games->group_start(); // only needed if you are adding OR'd query params
$games->like('name', $search);
$games->or_like('description', $search);
$games->group_end();
$games->order_by('name');
$games->get();
That’s a single query that handles querying all games for a given user based on a specific category. You can see how you can add additional query parameters as well.
It can be a more complicated issue when adding and removing connections:
// removing examples. Adding is similar
// easy first, remove the entire category
$user_category = $user->user_category->where_related('category', 'id', $category_id)->get();
// just delete the interim object, which removes all connections at once
$user_category->delete();
// removing a game from a category
$game = new Game($game_id);
$user_category = $user->user_category->where_related('category', 'id', $category_id)->get();
// just break the connection
$user_category->delete($game);
// harder, remove all copies of a game from a user
$game = new Game($game_id);
$user_category = $user->user_category->where_related('game', $game);
foreach($user_category as $uc) {
$uc->delete($game);
}
Well, that became much more extensive than I had originally planned.