database - 更新 liferay 数据库中的用户信息

标签 database liferay

我需要以编程方式更新数据库中现有用户的信息 我需要更新 Liferay 数据库中用户表中的用户名出生日期值 基本上我需要运行更新查询。

最佳答案

不建议直接更新 liferay 数据库,您应该改用 Liferay API 来做这些事情。根据 this liferay forum post :

The Liferay database is not published for a reason. The reason is the API does significantly more stuff than just simple SQL insert statements. There are internally managed foreign keys, there are things which are updated not just in the database but also in the indices, in jackrabbit, etc.

Since all of this is managed by the code and not by the database, any updates to the code will change how and when the database is updated. Even if it did work for you in a 6.1 GA1 version, GA2 is coming out in a couple of weeks and the database/code may change again.

Sticking with the API is the only way to insure the changes are done correctly.

好了说教好了,回到你的问题,这里有一些你可以做到的方法:

  1. 您可以构建自定义 portlet 并使用 liferay 的服务,并使用 UserLocalServiceUtil.updateUser() 方法更新用户名、生日等。

  2. 或者您可以构建一个基于 SOAP 或 JSON 的网络服务客户端来更新将调用相同方法的详细信息

  3. 或者您可以使用 Liferay's Beanshell tool要从控制面板执行此操作,以下是一些更新用户的代码(专为您创建ASAP):

    import com.liferay.portal.model.Company;
    import com.liferay.portal.model.Contact;
    import com.liferay.portal.model.ContactConstants;
    import com.liferay.portal.model.User;
    import com.liferay.portal.service.CompanyLocalServiceUtil;
    import com.liferay.portal.service.ContactLocalServiceUtil;
    import com.liferay.portal.service.UserLocalServiceUtil;
    
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    long companyId = 10135; // this would be different for you
    
    User user = UserLocalServiceUtil.getUserByEmailAddress(companyId, "test@liferay.com");
    
    // Updating User's details
    
    user.setEmailAddress("myTest@liferay.com");
    user.setFirstName("First Test");
    user.setLastName("Last Test");
    user.setScreenName("myTestScreenName");
    
    UserLocalServiceUtil.updateUser(user, false);
    
    // Updating User's Birthday
    
    // December 12, 1912
    int birthdayMonth = 11;
    int birthdayDay = 12;
    int birthdayYear = 1912;
    
    Calendar cal = new GregorianCalendar();
    cal.set(birthdayYear, birthdayMonth, birthdayDay, 0, 0, 0);
    cal.set(Calendar.MILLISECOND, 0);
    
    Date birthday = cal.getTime();    
    
    System.out.println("Updated User: " + user + "\nBirthdate to be updated: " + birthday);
    
    long contactId = user.getContactId();
    
    Contact contact = ContactLocalServiceUtil.getContact(contactId);
    
    if(contact == null) {
    
        contact = ContactLocalServiceUtil.createContact(contactId);
    
        Company company = CompanyLocalServiceUtil.getCompany(user.getCompanyId());
    
        contact.setCompanyId(user.getCompanyId());
        contact.setUserName(StringPool.BLANK);
        contact.setCreateDate(new Date());
        contact.setAccountId(company.getAccountId());
        contact.setParentContactId(ContactConstants.DEFAULT_PARENT_CONTACT_ID);
    }
    
    contact.setModifiedDate(new Date());
    contact.setBirthday(birthday);
    
    ContactLocalServiceUtil.updateContact(contact, false);
    
    System.out.println("Users birthdate updated successfully");
    

    联系人代码是借助 Liferay 的源代码构建的 UserLocalServiceImpl#updateUser方法

如果您想知道什么是 bean-shell 以及将此代码放在哪里,您可以在 Liferay 控制面板 控制面板 --> 服务器 --> 服务器管理 --> 脚本

Liferay Beanshell

关于database - 更新 liferay 数据库中的用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16392820/

相关文章:

Mysql - 创建触发器以根据插入到另一个表中的行插入新行

java - 当我尝试从 git 获取最新代码时,git 中出现 pull 时间错误

javascript - 在 Liferay 5.2.3 中禁用 JavaScript minifier 时出现问题

mysql - (部分)与mysql数据库同步

database - 如何计算 AWS DynamoDB 表的临时吞吐量?

ios - iOS 中的 SQLite 'database is locked' 错误

java - Liferay:消息的到期日期(留言板)

PHP 数据库 - 不想显示 javascript 代码

java - 创建 Liferay 插件项目时出错

java - 在java jsp中包含文件(svg)