Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display extra field of a many to many relationship inside a table

I have been searching for a while but didn't find any solution to do the following.

I am trying to display a table containing list of let's say Users. It's not a big deal to display fields from users but it is more complicated for association fields. Let's say I have the following tables : User, Post and UserPosts, in UserPosts we have a flag main to indicate whether a given post is the user's main position or not.

In my datatable, I have to display user's information, it means fields in user table + his/her main post.

So far I have something like this :

<table> 
    <thead> 
    <tr> 
        <th>name</th> 
        <th>FrstName</th>
        <th>Age</th> 
        <th>Main position</th>
        <th>Action</th> 
    </tr> 
        <tbody> 
           <c:forEach items="${listUsers}" var="user" varStatus="status">
            <tr>
                <td><c:out value="${user.name}" /></td> 
                <td><c:out value="${user.firstName}" /></td>
                <td><c:out value="${user.age}" /></td> 
                <td><c:out value="Position here" /></td> 
                <td>
                Delete
                <a href="/myAppli/user/${user.idUser}">Update</a>
                </td> 
            </tr> 
            </c:forEach>
        </tbody> 
</thead> 

I think for position I will have to retrieve from database main position of a given user in a given column, but then how to set this value (javascript, ajax?)

Am I thinking the wrong way, should I use the association class instead. By that I mean, retrieve all UserPosts where main field = true? And then iterate over userPosts instead of users, in that case, I will have something like this:

<c:forEach items="${listUserPosts}" var="userPost" varStatus="status">
        <tr>
            <td><c:out value="${userPost.user.name}" /></td> 
            <td><c:out value="${userPost.user.firstName}" /></td>
            <td><c:out value="${userPost.user.age}" /></td> 
            <td><c:out value="${userPost.post.postName}" /></td> 
            <td>
            Delete
            <a href="/myAppli/user/${userPost.user.idUser}">Update</a>
            </td> 
        </tr> 
</c:forEach>

It seems weird to me, maybe I'm wrong.

like image 383
aslan Avatar asked Nov 13 '22 14:11

aslan


1 Answers

If I understand the layout of your data correctly, you have something like this:

Data structure

If that's the case, you can structure your User object like this:

public class User {
  private int id;
  private String name;
  private String firstName;
  private int age;
  private Position mainPosition;
}

and your query would simply need to do a left join on the UserPosition that is marked as "main". That UserPosition would then join to the correct Position. Then your JSP would just need to check to see if your Position object was empty.

<c:forEach items="${listUsers}" var="user" varStatus="status">
  <tr>
    <td><c:out value="${user.name}" /></td> 
    <td><c:out value="${user.firstName}" /></td>
    <td><c:out value="${user.age}" /></td> 
    <td>
      <c:if test="empty ${user.position}">
        No main position
      </c:if>
      <c:if test="not empty ${user.position}">
        <c:out value="${user.position.title}" />
      </c:if>
    </td> 
    <td>
      Delete
      <a href="/myAppli/user/${user.idUser}">Update</a>
    </td> 
  </tr> 
</c:forEach>
like image 193
RustyTheBoyRobot Avatar answered Nov 15 '22 05:11

RustyTheBoyRobot