Hello!

I'm trying to develop a simple module that adds some custom information to an user. I followed a tutorial to integrate this module with Views, and this is the code I produced:

function factsegur_internal_users_views_data() {
  $data = array();

  $data['factsegur_internal_users_users_internal']['table']['group'] = t('Factsegur Internal Users');

  $data['factsegur_internal_users_users_internal']['table']['join']['users'] = array(
    'left_field' => 'uid',
    'field' => 'user_id',
  );

  // data entry for uid
  $data['factsegur_internal_users_users_internal']['user_id'] = array(
    'title' => t('Uid'),
    'help' => t('The User ID to whom this information relates.'), // The help that appears on the UI,

    // Information for displaying the uid
    'field' => array(
      'handler' => 'views_handler_field_user',
      'click sortable' => TRUE,
    ),

    // Information for accepting a uid as an argument
    'argument' => array(
      'handler' => 'views_handler_argument_user_uid',
      'name field' => 'User ID', // the field to display in the summary.
    ),

    // Information for accepting a uid as a filter
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),

    // Information for sorting on a uid.
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),

    'relationship' => array(
      'label' => t('Users'),
      'base' => 'users',
      'base field' => 'uid',
    ),
  );

  $data['factsegur_internal_users_users_internal']['name'] = array(
    'title' => t('Name'),
    'help' => t('Complete user\'s name'), // The help that appears on the UI,

    // Information for displaying the user's name
    'field' => array(
      'handler' => 'views_handler_field_user_name',
      'click sortable' => TRUE,
    ),

    'argument' => array(
      'handler' => 'views_handler_argument_string',
      'name field' => 'User name', // the field to display in the summary.
    ),

    'filter' => array(
      'handler' => 'views_handler_filter_string',
      'title' => t('Complete Name'),
      'help' => t('The user\'s complete name. This filter does not check if the user exists and allows partial matching. Does not utilize autocomplete.')
    ),

    'sort' => array(
      'handler' => 'views_handler_sort',
    ),

  );

  return $data;
}

When I go to the Views UI, it shows me the option to do the relationship between this modules information and the Users. I also show the correct listing of users when I check the "Require this relationship" box.

But when I add this module fields to the view (via Fields > Add > Complete Name), I get the following error:

SQLSTATE[42703]: Undefined column: 7 ERROR: column users_factsegur_internal_users_users_internal__factsegur_intern.uid does not exist LINE 1: ...gur_internal_users_users_internal__factsegur_int, users_fact... ^

The query that is executed is the following:

SELECT users.name AS users_name, users.uid AS uid, users_factsegur_internal_users_users_internal__factsegur_internal_users_users_internal.name AS users_factsegur_internal_users_users_internal__factsegur_int, users_factsegur_internal_users_users_internal__factsegur_internal_users_users_internal.uid AS users_factsegur_internal_users_users_internal__factsegur_int_1, users.created AS users_created
FROM 
{users} users
LEFT JOIN {factsegur_internal_users_users_internal} factsegur_internal_users_users_internal ON users.uid = factsegur_internal_users_users_internal.user_id
INNER JOIN {users} users_factsegur_internal_users_users_internal ON factsegur_internal_users_users_internal.user_id = users_factsegur_internal_users_users_internal.uid
LEFT JOIN {factsegur_internal_users_users_internal} users_factsegur_internal_users_users_internal__factsegur_internal_users_users_internal ON users_factsegur_internal_users_users_internal.uid = users_factsegur_internal_users_users_internal__factsegur_internal_users_users_internal.user_id
WHERE (( (users.status <> '0') ))
ORDER BY users_created DESC
LIMIT 5 OFFSET 0

I'm suspicious that the cause of the error is because the User ID is represented by uid in the users table and in my table is represented by user_id. But this should affect the result, I think.

I would appreciate any help.

Thanks and best regards!

Comments

cfreixo’s picture

Ok, after a couple of hour of "try and fail" I found the solution to this problem.

My particular issue was that I was using handlers that I should not be using. As far as I understood it (still not investigated very deeply), the views_handler_field_user_name uses searches for a uid field in the table you're using. I was not aware of that and used it incorrectly.

I hope this info will be of interest to someone out there :).