【问题标题】:search on custom field add in custom form in drupal搜索自定义字段在drupal中添加自定义表单
【发布时间】:2014-05-09 08:11:03
【问题描述】:

我们已经为在 drupal 中注册制作了一个自定义表单。 我们在其中添加了名字、姓氏、城市、技能。 现在我们要根据以下条件搜索用户。 但是,如果当 drupal 创建一个新字段时,它会在后端创建一个带有字段名称的表。 例如:field_data_field_account_type

like we have writtern code for search on te basis of account type

   function getUsersList($type=null){
$query = db_select('users', 'u');
$query->join('field_data_field_account_type', 'at', 'u.uid = at.entity_id'); 
$query->fields('at')
->fields('u')
->condition('at.field_account_type_value',$type,'=')
->orderBy('created', 'DESC');

$result = $query->execute();
$users = array();
while($record = $result->fetchAssoc()) 
{
    $user_fields = user_load($record['uid']);
    $users[] = $user_fields;
}
return $users;
   }


but if we will search on multiple tables using joins search will become slow.

那么,我们应该遵循什么方法来通过名字、城市、技能、 不使用连接。

drupal中有没有表单域api函数可以解决。

我找到了一种解决方案,但我面临一个问题。这是我的代码

      i found some solution for my problem but i am facing one problem              

    $query = new EntityFieldQuery();
     $results=$query->entityCondition('entity_type', 'user');
     if($type!=null)
     {
      $query->fieldCondition('field_account_type', 'value',$type, '=');
    }
           if($name!=null)
           {

 $query->fieldCondition('field_first_name', 'value',"%$name%", 'like');

//$query->fieldCondition('field_last_name', 'value',"%$name%", 'like');
           }

           /*if($city!=null&&isset($city))
           {
           $query->fieldCondition('field_city', 'value',"%$city%", 'like');
           }
            if($skill!=null&&isset($skill))
           {
    $query->fieldCondition('field_skills', 'value',"%$skill%", 'like');
            }*/
           $results->execute();


This code only work on first name , bu not for last name.lIKE here and condition is working for last name 

所以我可以设计这样的查询,使用或其中只有一个文本框.seach user by firstname,lastname,city,skill etc.

【问题讨论】:

    标签: php drupal drupal-7


    【解决方案1】:

    首先我要说的是我对drupal一无所知,但我会提出不同的观点。 你的问题是加入真的很慢,所以你想要一个非加入的解决方案。

    我的想法是提高加入速度而不是不使用它,因为我认为在你的情况下“加入”是一个相当合理的事情。

    为了加快速度,我建议在用户表中添加 uid 索引。还有 field_data_field_account_type 中的 entity_id 和 field_account_type_value。

    如果您之前没有为这 3 列建立索引,那么您的查询速度应该会大大提高。

    【讨论】:

    • 我为我的问题找到了一些解决方案,但我面临一个问题
    • 对不起,我完全不明白你的评论。如果您已经有索引,如果我的答案不起作用,那也没关系。
    【解决方案2】:

    我没有得到任何解决方案,所以我将用户的数据复制到另一个表中,以便快速搜索。

    我的代码是:

    function users_user_insert(&$edit, $account, $category) {
    $coFounder      =   '';
     $founder       =   '';
     if($account->field_account_type['und'][0]['value']=='co-founder'){
    $coFounder      =   $account->field_account_type['und'][0]['value'];
     }
     else if(($account->field_account_type['und'][0]['value']=='founder')&&($account-    
       >field_account_type['und'][1]['value']=='co-founder'))
      {
    $founder        =   $account->field_account_type['und'][0]['value'];
    $coFounder      =   $account->field_account_type['und'][1]['value'];
     }
     else{
    $founder        =   $account->field_account_type['und'][0]['value'];
      }
    
    
       $result = db_insert('lew_users_meta')->fields(array('uid'=>$account-    
       >uid,'name'=>$account->name,'first_name'=>$account->field_first_name['und'][0]
       ['value'],
    'last_name'=>$account->field_last_name['und'][0]['value'],'mail'=>$account->mail,
    'city'=>$account->field_city['und'][0]['value'],
         'skills'=>$account->field_skills['und'][0]['value'],
    
    
              'coFounder'=>$coFounder,'founder'=>$founder,'created'=>time()
                            )
    
    
                           )                 
                           ->execute();
    
             }
    
         function users_user_update(&$edit, $account, $category) {
         $coFounder     =   '';
         $founder       =   '';
        if($account->field_account_type['und'][0]['value']=='co-founder'){
    $coFounder      =   $account->field_account_type['und'][0]['value'];
         }
     else if(($account->field_account_type['und'][0]['value']=='founder')&&($account-   
      >field_account_type['und'][1]['value']=='co-founder'))
     {
    $founder        =   $account->field_account_type['und'][0]['value'];
    $coFounder      =   $account->field_account_type['und'][1]['value'];
     }
     else{
    $founder        =   $account->field_account_type['und'][0]['value'];
      }
    
        $result = db_update(' lew_users_meta')
                           ->fields(array('name'=>$account->name,'first_name'=>$account-    
          >field_first_name['und']['0']['value'],
    'last_name'=>$account-     
        >field_last_name['und'][0]['value'],'mail'=>$account->mail,
    'city'=>$account->field_city['und'][0]['value'],
        'skills'=>$account->field_skills['und']['0']['value'],
    
          'founder'=>$founder,'coFounder'=>$coFounder,'modified'=>time()
                            )
    
    
                           )
                           ->condition('uid', $account->uid,'=')
                           ->execute();
    
    
    
      }
    

    所以我们使用钩子在另一个表中插入数据。所以我们不应该加入
    表。

    如果您有更好的解决方案,请在此处发布

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多