I have a "legacy" table I need to join to a custom content type. I
have already joined this table to {users} with no problem, but I
cannot join this to {node}. I tried specifying the cck table, but
without luck, after adding a field of my legacy table, views preview
returns an ajax error.

Any hint? thanks

 $data['tb_afiliats']['table']['join'] = array(
   'users' => array(
     'left_table'  => 'alt_login',
     'left_field' => 'alt_login',
     'field' => 'afiliat_dni', //legacy field
   ),
   'node' => array(
//      'left_table'  => 'content_field_nif',
     'left_field' => 'field_nif_value',
     'field' => 'afiliat_dni', //legacy field
   ),
 );

Comments

enboig’s picture

After some testing I found Views isn't joining the table:

Warning: Unknown column 'afiliat_dni' in 'field list' query: execute /* admin : execute */ SELECT DISTINCT node.nid AS nid, node_data_field_alumne.field_alumne_nid AS node_data_field_alumne_field_alumne_nid, node.language AS node_language, node.type AS node_type, node.vid AS node_vid, node_data_field_nif.field_nif_value AS node_data_field_nif_field_nif_value, afiliat_dni FROM pef_node node LEFT JOIN pef_content_field_alumne node_data_field_alumne ON node.vid = node_data_field_alumne.vid LEFT JOIN pef_content_field_nif node_data_field_nif ON node.vid = node_data_field_nif.vid WHERE node.type in ('afiliat') LIMIT 0, 10

Is there any problem in my table definition?

dawehner’s picture

 $data['tb_afiliats']['table']['join'] = array(
   'users' => array(
     'left_table'  => 'alt_login',
     'left_field' => 'alt_login',
     'field' => 'afiliat_dni', //legacy field
   ),
   'node' => array(
      'left_table'  => 'content_field_nif',
     'left_field' => 'field_nif_value',
     'field' => 'afiliat_dni', //legacy field
   ),
 );


Is 'afiliat_dni' a field of the table tb_afiliats?

This field should have this.

enboig’s picture

yes, it is.

enboig’s picture

The problem continues:

  $data['tb_afiliats']['table']['group'] = 'tb_afiliats';
  $data['tb_afiliats']['table']['join'] = array(
    // Index this array by the table name to which this table refers.
    // 'left_field' is the primary key in the referenced table.
    // 'field' is the foreign key in this table.
    'users' => array(
      'left_table'  => 'alt_login',
      'left_field' => 'alt_login',
      'field' => 'afiliat_dni',
    ),
    'nodes' => array(
      'left_table'  => 'content_field_nif',
      'left_field' => 'field_nif_value',
      'field' => 'afiliat_dni',
    ),
  );
  // Next, describe each of the individual fields in this table to Views. For
  // each field, you may define what field, sort, argument, and/or filter
  // handlers it supports. This will determine where in the Views interface you
  // may use the field.

  // Node ID field.
  $data['tb_afiliats']['afiliat_id'] = array(
    'title' => 'afiliat_id', //trans
    // Because this is a foreign key to the {node} table. This allows us to
    // have, when the view is configured with this relationship, all the fields
    // for the related node available.
    'help' => 'id de l\'afiliat a la taula tb_afiliats',
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
     ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

The generated query is:

SELECT DISTINCT node.nid AS nid, node.title AS node_title, node.language AS node_language, node_data_field_nif.field_nif_value AS node_data_field_nif_field_nif_value, node.type AS node_type, node.vid AS node_vid, afiliat_dni FROM pef_node node LEFT JOIN pef_content_field_alumne node_data_field_alumne ON node.vid = node_data_field_alumne.vid LEFT JOIN pef_node node_node_data_field_alumne ON node_data_field_alumne.field_alumne_nid = node_node_data_field_alumne.nid LEFT JOIN pef_content_field_nif node_data_field_nif ON node.vid = node_data_field_nif.vid INNER JOIN pef_node_access na ON na.nid = node.nid WHERE ...

The reason table pef_content_field_nif appears is because I requested "node_data_field_nif.field_nif_value" field; if I don't the table don't appear.

Any hint? Maybe I should join my table to another table so CCK takes it?

R.Hendel’s picture

Status: Active » Fixed
Issue tags: +dvcs11

Hi, is this request still active? In the meantime views has continued so much further.

I would fix this to clean queue.
Please feel free to reopen it, if you can provide more information.
Otherwise it would be closed automatically after two weeks.

enboig’s picture

I used hook_views to define the tables in a module and I coded everything manually; but in further cases it would be usefull to have it solved.

Status: Fixed » Closed (fixed)
Issue tags: -dvcs11

Automatically closed -- issue fixed for 2 weeks with no activity.