Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Natalie D
How search users by expando?
April 5, 2012 9:48 AM
Answer

Natalie D

Rank: Junior Member

Posts: 55

Join Date: February 6, 2012

Recent Posts

Hi

Does somebody know how to write DynamicQuery that search through the database also by custom fields (AKA expando fields)?
My main problem is that custom fields aren't additional columns inside table that they are extending.

I do not even know whether it is possible or not. Any ideas folks?

Natalie
André Bunse
RE: How search users by expando?
April 5, 2012 5:42 PM
Answer

André Bunse

Rank: Junior Member

Posts: 85

Join Date: March 15, 2012

Recent Posts

Hi Natalie,

i have no answer using "DynamicQuery", but "search users by expando" can be done with "ExpandoValueLocalServiceUtil"

here is a snippet to get all users with custom field "newsletter"

 1    //  Set search parameter   
 2    String customAttributeName = "newsletter";
 3    long companyId = PortalUtil.getDefaultCompanyId();
 4    long classNameId = ClassNameLocalServiceUtil.getClassNameId(User.class);
 5   
 6    List<ExpandoValue> values = ExpandoValueLocalServiceUtil.getColumnValues(
 7            companyId,
 8            classNameId,
 9            ExpandoTableConstants.DEFAULT_TABLE_NAME,
10            customAttributeName,
11            -1,
12            -1
13            );
14   
15    // create an arraylist to store user objects
16    List<User> users = new ArrayList<User>();
17    // temp user object
18    User user;      
19
20    // iterate through list of ExpandoValues and for each
21    // element try to find corresponding user object
22    for(int i = 0; i < values.size(); i++) {
23      long userId = values.get(i).getClassPK();
24      try {
25        user = UserLocalServiceUtil.getUser(userId);
26        users.add(user);
27      }
28      catch(NoSuchUserException e) {
29         // user with this primary key was not found in DB          
30      }
31    }



HTH
André
David H Nebinger
RE: How search users by expando?
April 5, 2012 8:33 PM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6500

Join Date: September 1, 2006

Recent Posts

The user object is not bound w/ the expando objects directly, so you cannot build a DQ that would do what you're after.

Andre's answer is the one that you'll need to go with.
Riccardo Ferrari
RE: How search users by expando?
April 6, 2012 1:21 AM
Answer

Riccardo Ferrari

Rank: Regular Member

Posts: 137

Join Date: November 13, 2010

Recent Posts

Hi,

You can use DynamicQuery to search user by their Expando attributes. You can use DynamicQuery to develop query joins (it get translated into "select ... from (select ...)"), here is the post where I started from:
http://www.liferay.com/community/forums/-/message_boards/message/1817322

The drawback is that DynamicQuery on Expando attribute are very poor in performances (I think due to lack of index on ExpandoValue.data_)

Regards
Natalie D
RE: How search users by expando?
April 6, 2012 1:23 AM
Answer

Natalie D

Rank: Junior Member

Posts: 55

Join Date: February 6, 2012

Recent Posts

All Your responses are extremely helpful.

I'll come back after Easter and share my experience trying both ways.

Regards!
Natalie
Atul Patel
RE: How search users by expando?
October 10, 2012 7:10 AM
Answer

Atul Patel

Rank: New Member

Posts: 21

Join Date: January 12, 2012

Recent Posts

Has anyone tried to search via the lucene index instead?

The idea is that if you know you may need to search for this attribute in the future, modify the userIndexer to add the attribute to lucene... and then use the lucene search mechanism instead of db query.

I'll try it when I get a chance but wanted to put this out there in case it helps someone before I get a chance to try this out.
Alex Curtui
RE: How search users by expando?
November 12, 2012 3:59 AM
Answer

Alex Curtui

Rank: Junior Member

Posts: 30

Join Date: November 8, 2012

Recent Posts

Atul Patel:
Has anyone tried to search via the lucene index instead?

The idea is that if you know you may need to search for this attribute in the future, modify the userIndexer to add the attribute to lucene... and then use the lucene search mechanism instead of db query.

I'll try it when I get a chance but wanted to put this out there in case it helps someone before I get a chance to try this out.



Have you tried it? If so, please share the process.

Thank you!