Al.Boldyrev
Al.Boldyrev

Reputation: 486

Service Builder Liferay Relationships

I have "Storage" portlet: enter image description here

I can click "Add Book":

enter image description here

But I need to have opportunity to add author's name to my book. So I created new project, new Service Builder with new entity "author", and now I can add author too. But how can I make dropdown menu with existing authors when I click "add book"? How can I bind that field with new table - "author"?

Upvotes: 1

Views: 453

Answers (1)

KlajdPaja
KlajdPaja

Reputation: 959

You don't need to create another project for the author, in the same service builder as the book entity add author entity and add reference to another table that will hold the primary key's of book and author. service.xml could be:

<service-builder package-path="com.myproject.db">
<author>SO</author>
<namespace>myp</namespace>
<entity name="Book" local-service="true" remote-service="true" cache-enabled="false">
<column name="bookId" type="long" primary="true" />
<column name="name" type="String" />
<column name="description" type="String" />
<column name="price" type="long" />
<column name="author" type="Collection" entity="Author" mapping-table="Author_Books"/>
</entity>
<entity name="Author" local-service="true" remote-service="true" cache-enabled="false">
<column name="authorId" type="long" primary="true" />
<column name="authorName" type="String" />
<column name="books" type="Collection" entity="Book" mapping-table="Author_Books" />
</entity>
</service-builder>

And when you deploy this portlet it will create another table myp_Author_Books that will have composite key of authorId and bookId. Than in your rendering method of book form add

List<Author> authorList=AuthorLocalServiceUtil.getAuthor(0, AuthorLocalServiceUtil.getAuthorCount()); renderRequest.addAttribute("listAuthor",authorList )

and use this attribute in jsp with c:forEach and aui:select to create your dropdown, smth like:

<aui:select name="author" label="Author Name">
    <c:forEach var="currAuthor" items="${listAuthor}">
        <aui:option value="${currAuthor.authorId}" label=" ${student.authorName}"></aui:option>
    </c:forEach>
</aui:select>

Upvotes: 5

Related Questions