Hi, thanks for this module, it's very helpful.
I found a bug while creating a view, I needed the view to return the oldest nid for each gallery so I added a relationship but it had no effect.

After inspecting the resulting query I found that the its returning the following:

SELECT node.nid AS nid,
   node_node_gallery_galleries.nid AS node_node_gallery_galleries_nid
..

node_node_gallery_galleries is the name of the first relationship, as you can see it's returning the nid field, however in the relationship definition the name of the field with the max (or min) nid has a different alias:

SELECT gid, MAX(ngi.nid) AS maxnid, node.*

So these relationships aren't producing any effect, I could solve this making the following change:

SELECT gid, MAX(ngi.nid) AS nid 

I'm attaching a patch that fixes this,
Hope you find it useful.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

javi-er’s picture

FileSize
1.38 KB

sorry for the double post.

justintime’s picture

Version: 6.x-3.0-alpha1 » 6.x-3.x-dev
Priority: Major » Normal
Status: Active » Needs review

updating metadata

scroogie’s picture

Isn't it on purpose to name it differently to not conflict? It's a subquery after all. Perhaps a 'field' => 'maxnid' or something along those lines helps?

javi-er’s picture

Well the nid field name will not conflict with the main query nid since Views adds the table alias before each field.

I tried what you mention and unfortunately didn't work, I believe that actually the subquery needs to return the original field name in order to work, but then I don't know Views API that deep to be sure about this.

Not sure if there is another property that can be used for defining the field name but I couldn't find any.

justintime’s picture

Pretty sure this is accurate. I have to admit that I don't remember the last time (if ever) I tested those relationships. I have verified the bug exists, but haven't tested the fix yet. I'll try to get some time this week to do some patching/committing.

javi-er’s picture

Cool, I'll be happy to test it after you implement the patch it you need.

justintime’s picture

Status: Needs review » Needs work

The patch doesn't work for me either. I'll see if I can't dig into it later tonight and see what's up. Both before and after return a record, but it's not the appropriate one.

scroogie’s picture

Status: Needs work » Needs review
FileSize
1.94 KB

The problem seemed to be that the node.* overwrote the field again. It's required to be able to return all fields though, so I wrote it out except for nid.

This one works for me.

justintime’s picture

Status: Needs review » Needs work

I thought about doing it that way too. It works, but has a major problem. What happens when a contrib module adds a field to the node table? Or node.module from core adds a field at some point? We'll miss it.

If we're going this route, we should at least fetch the schema, and iterate over that eliminating the nid column. If you're busy, I can do that too. I just wanted someone else to arrive at the same (hacky) fix I came up with before I attempted to code it up :)

scroogie’s picture

I see the point, but do you really think this is worth the effort?
If core adds a column that will be exported as a views field (which it wouldn't at this point), we could release an upgrade.

justintime’s picture

FileSize
1.98 KB

Effort is pretty minimal. I'm attaching a patch that does this.

However, I think I'm doing something wrong. With this, and every other patch in this issue, when I setup the relationship and use a field from it, I do get a result, but it's always the *incorrect* result -- ie it's not the newest or oldest image. Can someone confirm or deny?

scroogie’s picture

I had the same effect with the other patches, except mine. I got the correct images with that one.

justintime’s picture

Well, now I'm really confused, because I modified your patch to make mine. In fact, we get the same SQL, we just get it in different ways?!?!?

scroogie’s picture

Well, I just tried it out. Your patch works for me as well. Doesn't it for you?

justintime’s picture

Yeah, none of them work. Maybe I'm doing something wrong in the view. Can you post yours so I can compare to what I have?

scroogie’s picture

Sure thing. Are you on Views 3 alpha? Perhaps its another incompatible change. Here is my view:

$view = new view;
$view->name = 'Testview_1032986';
$view->description = 'Newest and oldest test';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
  'newest_image' => array(
    'label' => 'Gallery Newest Image',
    'required' => 0,
    'id' => 'newest_image',
    'table' => 'node_gallery_galleries',
    'field' => 'newest_image',
    'relationship' => 'none',
  ),
));
$handler->override_option('fields', array(
  'title' => array(
    'label' => 'Gallery title',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
  ),
  'title_1' => array(
    'label' => 'Newest image title',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title_1',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'newest_image',
  ),
  'nid' => array(
    'label' => 'Maximum NID:',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'relationship' => 'newest_image',
  ),
  'nothing' => array(
    'label' => 'Custom text',
    'alter' => array(
      'text' => '----------------------------------------------------------------',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'exclude' => 0,
    'id' => 'nothing',
    'table' => 'views',
    'field' => 'nothing',
    'relationship' => 'none',
  ),
));
$handler->override_option('filters', array(
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'node_gallery_gallery' => 'node_gallery_gallery',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->override_option('path', 'newest');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
justintime’s picture

Status: Needs work » Fixed

Hmm, don't know what I was doing wrong, but your view works for me. I'll commit this and move on with life :)

javi-er’s picture

Nice to this commited :-) both patches worked fine for me.

Thanks for taking a look to it!

Status: Fixed » Closed (fixed)

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