Adding custom field to User programmatically through liferay.expando

假如想象 提交于 2019-12-12 03:43:11

问题


I am trying to add fields to com.liferay.portal.model.User, an extra attribute using Expando. Can someone explain to me how this method is adding a field because docs don't have much description.

private void addUserCustomAttribute(long companyId, ExpandoTable userExpandoTable, String attributeName, int type) throws PortalException, SystemException {

    ExpandoColumnLocalServiceUtil.getColumn(userExpandoTable.getTableId(), attributeName); //should be addColumn(long tableId, String name, int type) ???

} //and where can find type description couse i have very specific type, Map(String,Object) couse in ExpandoColumnConstants didn't see it

I have taken this from Liferay Expando Wiki's Adding User Custom Attributes.

When should I call this all? Where to put this in my project? What change is required or everything needs to be changed to call it.

Some good tutorial will be nice because it's hard to find something from 0 to end, always found only some part with no explanation.


回答1:


The question is not very clear. But if you simply want to add a custom attribute for your User then you can refer to my answer here and reproduced for your reference:

Custom field for the user-entity can be created through:
Control Panel -> Portal -> Custom Fields -> User.

And programmatically can be created as follows:

user.getExpandoBridge().addAttribute("yourCustomFieldKey");

Then set the value as:

user.getExpandoBridge().setAttribute("yourCustomFieldKey", "valueForCustomField");

If your custom field is already present you can check like this:

if (user.getExpandoBridge().hasAttribute("yourCustomFieldKey")) { ... };

The data is stored in tables prefixed with "EXPANDO":

  • EXPANDOCOLUMN: stores the custom field key and other settings (contains the tableId refrences)
  • EXPANDODATA: stores the custom field value for the key (contains the columnId and tableId refrences)
  • EXPANDOTABLE: stores for which liferay entity (user) are you adding the custom field
  • EXPANDOROW: stores linking information between a user and its values (contains tableId and userId refrences)

Hope this helps.




回答2:


If your custom field is multivalue, you can use this:

String customVal = "yourCustomFieldValue";

user.getExpandoBridge().setAttribute("yourCustomFieldKey", new String[] {customVal }, false);

The last parameter set to "false" avoids permission check.



来源:https://stackoverflow.com/questions/11558576/adding-custom-field-to-user-programmatically-through-liferay-expando

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!