2

I've got two tables: {students} and {student_names}. Students is the base table, with a primary key of {students}.sid. Student names is the joined table, and has a key of {student_names}.sid.

I've defined my join in hook_views_data:

$data['students']['table']['join']['student_names'] = [
  'left_field' => 'sid',
  'field' => 'sid',
];

I want to select the given name of the student from the joined table. I've defined the field as follows (again in hook_views_data()):

$data['students']['given_name'] = [
  'title' => t('Given name'),
  'help' => t('The given (first) name of the student'),
  'field' => [
    'handler' => 'views_handler_field',
   'click sortable' => TRUE,
  ],
  'sort' => [
    'handler' => 'views_handler_sort',
  ],
  'filter' => [
    'handler' => 'views_handler_filter_string',
  ],
  'relationship' => [
    'base' => 'student_names',
    'field' => 'sid',
    'handler' => 'views_handler_relationship',
  ],
];

I'm getting this error:

Column not found: 1054 Unknown column 'students.given_name' in 'field list'

How can I tell views that the column is in the {student_names} table and not the {students} table?

  • 1
    You might need to try something like $data['students']['sid'] instead of $data['students']['given_name'] and then add relationship to student_names table, and then get the given name. Like we get the Author names for Content. – Yogesh Dec 19 '16 at 9:56
1

I solved my own issue.

I needed to expose the joined table to views as well. So on top of setting up $data['students'], I also needed to set up $data['student_names'], with a join back to the original table. Here is my hook_views_data() in its entirity:

$data['student_names']['table'] = [
  'group' => t('Student'),
  'base' => [
    'field' => 'sid',
    'title' => t('Student'),
  ],
  'join' => [
    'students' => [
      'left_field' => 'sid',
      'field' => 'sid',
      'type' => 'inner',
    ],
  ],
];

$data['student_names']['given_name'] = [
  'title' => t('Given name'),
  'help' => t('The given (first) name of the student'),
  'field' => [
    'handler' => 'views_handler_field',
    'click sortable' => TRUE,
  ],
];

return $data;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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