Simple view (either user or node) using relation on User Flag generates blank fields on output since apparently wrong field used in JOIN. Export of user view is listed below.

Steps to recreate

  1. create user flag & flag a few users
  2. create view - either user or node view
  3. add relation on user flag
  4. add user fields such as name - note relation is not asked for
  5. missing something?

Generated Query:

SELECT users.uid AS uid,
   users.name AS users_name
 FROM users users 
 INNER JOIN flag_content flag_content_users ON users.uid = flag_content_users.content_id AND flag_content_users.fid = 4

Changing "ON users.uid = flag_content_users.content_id" to "ON users.uid = flag_content_users.uid" generates desired result using phpMyAdmin Run SQL query.

Exported view:

$view = new view;
$view->name = 'test';
$view->description = 'test';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'users';
$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(
'flag_content_rel' => array(
'label' => 'Contributor flag',
'required' => 1,
'flag' => 'contributor',
'user_scope' => 'any',
'id' => 'flag_content_rel',
'table' => 'users',
'field' => 'flag_content_rel',
'relationship' => 'none',
),
));
$handler->override_option('fields', array(
'name' => array(
'label' => 'Name',
'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_user' => 1,
'overwrite_anonymous' => 0,
'anonymous_text' => '',
'exclude' => 0,
'id' => 'name',
'table' => 'users',
'field' => 'name',
'relationship' => 'none',
),
));
$handler->override_option('access', array(
'type' => 'none',
));
$handler->override_option('cache', array(
'type' => 'none',
));

Comments

mooffie’s picture

Category: bug » support

There's no problem in the generated query.

You didn't explain what you want to do.

4. add user fields such as name - note relation is not asked for

Relation is not asked for because there's only one user table in your view: that of the flagged user. If you want to see details about the flagging user, pull in the "Flags: User" relationship. Please see a short guide to using the Views integration: note the section "The flagging user" (However, when you encounter "The user who authored the content" read it as if it were "The flagged user", because in your case you're flagging users, not nodes).

k8’s picture

Category: support » bug

Thx but cannot get this to work with what Views generates.
I want a list of the flagged users where they flag themselves, indicating they want to be considered for a certain role.

mooffie’s picture

There's no bug here. You're looking for a feature. Views isn't an SQL editor: there are many queries you can't do with it out of the box.

I want a list of the flagged users where they flag themselves

You can't do this with just our Views support (AFAIK).

they flag themselves, indicating they want to be considered for a certain role.

You can use a separate flag, "I want to volunteer". (Perhaps that's even a better idea than you original one, because volunteering yourself is quite different than recommending others.)

Currently there's isn't a "Users can only flag themselves" setting, but you can implement hook_flag_access.

k8’s picture

As first mentioned changing "ON users.uid = flag_content_users.content_id" to "ON users.uid = flag_content_users.uid" generates desired result which is a list of users who have been flagged or did the flagging - same thing since I give them a link to flag themselves only via a call to flag_create_link('contributor', $uid);
Why isn't this a bug since content_id is should not be used as the field to match re uid?
It seems quite straightforward to ask for all the flagged users in a user view. In my testing where 3 users are flagged, the 3 records are found and listed with empty fields until I change the query to match on uid. If this is not available by design then documentation should say so but I feel it could and should be, esp. with a user view (same code seems to be generated whether user view or node view).

mooffie’s picture

Category: bug » support

As first mentioned changing "ON users.uid = flag_content_users.content_id" to "ON users.uid = flag_content_users.uid" generates desired result which is a list of users who have been flagged

If your desired result is the list of users who have been flagged, then the query Views generates is the correct one:

We're starting with all the records in {user}. The "inner join" matches every user with the record(s) that flag(s) him. Since it's an "inner join", users who aren't flagged are filtered out. So we're left with all the flagged users.

or did the flagging - same thing since I give them a link to flag themselves only

Well, why did you kept this detail a secret?

If users can flag only themselves, then indeed your modified query too will work.

But since the original Views query doesn't work for you, it means that something is broken:

flag_create_link('contributor', $uid);

Perhaps during the development '$uid' didn't always point to the current user. You need to examine the {flag_content} table, using your phpMyAdmin, and ensure that its 'uid' column always equals its 'content_id' column (because users can only flag themselves). If this isn't the case, you know your '$uid' php variable wasn't/isn't set correctly. Do <?php print $uid; ?> to see its value.

It seems quite straightforward to ask for all the flagged users in a user view.

Sure. That's what the query does.

mooffie’s picture

Status: Active » Fixed

As I said, you should examine your {flag_content} table and make sure the data isn't corrupted (that is, that columns 'uid' and 'content_id' are equal). Please re-open this issue if you encounter problems.

Status: Fixed » Closed (fixed)

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