3

I have created Search page which searches Products (Content type) by selecting few option drop down menus (those are selecting taxonomy terms), but also i have an text input field, which is supposed to narrow or define search by keywords.

Now problem is that i need to search several fields at same time.

This script works perfectly (snippet):

$keywords=$_POST ['keywords'];
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'product')
->fieldCondition('field_main_category', 'tid', $category, 'IN') 
->fieldCondition('field_sku', 'value','%'. $keywords .'%', 'LIKE'); 
$result = $query->execute();

I got all the product that are matching user entry, even if partial (those %). But i need to query some other fields too (description and name), to search for terms that user may enter.

But, problem is that i must have one input field, and user may target 3 different fields, so three fieldConditions for same term will cause nothing to be found, because selection is excluding all if term is not found in one field.

Could I,some way, write EntityFieldQuery query in a way to tell it to "forget" condition if keyword is not found ? (like in bellow example)

$keywords=$_POST ['keywords'];
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'product')
->fieldCondition('field_main_category', 'tid', $category, 'IN') 
->fieldCondition('field_sku', 'value','%'. $keywords .'%', 'LIKE'); 
->fieldCondition('field_name', 'value','%'. $keywords .'%', 'LIKE'); 
->fieldCondition('field_description', 'value','%'. $keywords .'%', 'LIKE'); 
$result = $query->execute();

Other workaround could be to look all three fields in one place, if i could get combined fields in one condition query (this could be done by hand, by adding another field only for search terms that will be filled with tags that will be searchable summary of all 3 fields, but i am hoping to find the way not to create more field and content, if possible).

Any suggestions or experiences with that ? Thanks!

1

1 Answer 1

1

Do you mean something like this;

$keywords=$_POST ['keywords'];

$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'product')
->fieldCondition('field_main_category', 'tid', $category, 'IN');

if(isset($keywords)) {
 $query  
   ->fieldCondition('field_sku', 'value','%'. $keywords .'%', 'LIKE');
} 
$result = $query->execute();

UPDATE

You can do an OR for each of the field. SO to do this you add a tag to the query which you are building and then you can alter the query using the hook_query_TAG_alter

    $keywords=$_POST ['keywords'];

    $query = new EntityFieldQuery();
    $entities = $query->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'product')
    ->addTag('something');

    $result = $query->execute();


function modulename_query_**something**_alter(QueryAlterableInterface $query) {
  $or = db_or()
    ->fieldCondition('field_sku', 'value','%'. $keywords .'%', 'LIKE')
    ->fieldCondition('field_name', 'value','%'. $keywords .'%', 'LIKE');

  $query
      ->condition($or);
}

This is just and example and you will have to make changes as per your requirements but this OR will definitely help.

Refer to the following links for a good example; http://treehouseagency.com/blog/tim-cosgrove/2012/02/16/entityfieldquery-let-drupal-do-heavy-lifting-pt-1 http://treehouseagency.com/blog/neil-hastings/2011/09/06/building-energygov-without-views http://treehouseagency.com/blog/fredric-mitchell/2012/02/21/or-queries-entityfieldquery

6
  • I am not searching exactly for this, as three fields must take $keywords variable. ($keywords is set and must be checked against 3 fields, and if not found in some field, somehow tell query not to take that condition in account). But it opens a way to new approach for me, in some conditions (I was not aware i can split queries in this way!) upovoted, thanks! I update my question to clarify a bit more what i am searching for.
    – NenadP
    Oct 11, 2012 at 7:37
  • @HoverFusion I have updated the answer please have a look.
    – junedkazi
    Oct 11, 2012 at 15:21
  • This is definately good lead to the answer. Is there anything that could prevent doing this hook from template file ( iwould like to do it from template.php) ? And i think you do not need semicolon after first fieldCondition.
    – NenadP
    Oct 12, 2012 at 14:50
  • 1
    @HoverFusion updated the answer about the semi-colon but I am not sure if it will work at the template level have never tried it before. But give it a try and let me know your findings.
    – junedkazi
    Oct 12, 2012 at 15:40
  • 1
    No luck from template side, i am doing module implementation. but got this error: Fatal error: Call to undefined method DatabaseCondition::fieldCondition()
    – NenadP
    Oct 13, 2012 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.