I get an error when trying to view /user/1/friends

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.uidd = '1') OR (users.uidINSELECTf.uidFROMflag_friendfWHEREf.friend_uidd = '1')' at line 2: SELECT COUNT(*) AS expression FROM (SELECT users.uid AS uid, 1 AS expression FROM {users} users LEFT JOIN {flag_friend} flag_friend ON users.uid = flag_friend.uid WHERE (( (users.status <> :db_condition_placeholder_0) )AND( (users.uidINSELECTf.friend_uidFROMflag_friendfWHEREf.uidd = :db_condition_placeholder_1) OR (users.uidINSELECTf.uidFROMflag_friendfWHEREf.friend_uidd = :db_condition_placeholder_2) )) GROUP BY uid) subquery; Array ( [:db_condition_placeholder_0] => 0 [:db_condition_placeholder_1] => 1 [:db_condition_placeholder_2] => 1 ) in views_plugin_pager->execute_count_query() (line 141 of /Users/jbitner/Sites/drupal-7.x-dev/sites/all/modules/views/plugins/views_plugin_pager.inc).

Which totally makes no sense to me... all this debug info is crammed together...

Comments

sirkitree’s picture

Title: Views are broken » Views are broken in D7

Making it more obvious to the casual browsers of issues.

jyee’s picture

I've attached a patch to includes/flag_friend_handler_argument_numeric.inc that resolves the errors above.
apparently add_where() is stripping spaces from the $field arg, so the patch simply moves the rest of the subselect to the $value arg

There's still a problem where the "View All Friends" tab doesn't display friends.
The "Awaiting Friend Approvals" and "Friend Requests" tabs appear to be working as intended.

edit: see following post

jyee’s picture

Status: Active » Needs review
StatusFileSize
new1.9 KB

ah... nevermind the previous post and patch (spoke too soon).

This patch resolves the error message and also presents a working all friends view.

Edit: the patch also has a leftover comment and call to devel's dd() that should probably be removed... sorry for not cleaning up after myself.

jon pugh’s picture

Worked for me.

++

sirkitree’s picture

Anonymous’s picture

Status: Fixed » Active

Well the patch does not work for me! The error message is gone, but the "View All Friends" view display is empty. I double checked that the user has friends. Digging in the query I found out that the "… users.uid = 'IN (…" does not return a value while "… users.uid IN (…" does. Getting rid of the = and the ' ' around the subselect does the trick for me. I tried to fix this in the argument_numeric.inc file, but with little luck. It seams views add_where() function puts the = and the ' ' in by default.

SELECT users.picture AS users_picture, users.uid AS uid, users.name AS users_name, users.mail AS users_mail, flag_friend.uid AS flag_friend_uid
FROM {users} users
LEFT JOIN {flag_friend} flag_friend ON users.uid = flag_friend.uid
WHERE (( (users.status <> '0') )AND( (users.uid = 'IN (SELECT f.friend_uid FROM {flag_friend} f WHERE f.uid = 71)') OR (users.uid = 'IN (SELECT f.uid FROM {flag_friend} f WHERE f.friend_uid = 71)') ))
GROUP BY uid
LIMIT 10 OFFSET 0
sirkitree’s picture

Hrm, yeah - seems like we need a different way to construct this. To be clear, the way we have it now, the WHERE that is generated is this:

... 
WHERE (( (users.status <> '0') )
AND( 
  (users.uid = 'IN (SELECT f.friend_uid FROM flag_friend f WHERE f.uid = 37)') 
  OR 
  (users.uid = 'IN (SELECT f.uid FROM flag_friend f WHERE f.friend_uid = 37)') )) 
...

created by:

...
    else {
      $operator = empty($this->options['not']) ? '=' : '!=';
      $this->query->add_where($group, 'users.uid', "IN (SELECT f.friend_uid FROM {flag_friend} f WHERE f.uid $operator $this->argument)");
      $this->query->add_where($group, 'users.uid', "IN (SELECT f.uid FROM {flag_friend} f WHERE f.friend_uid $operator $this->argument)");
    }

but what we really want is this:

...
WHERE (( (users.status <> '0') )
AND( 
  (users.uid IN (SELECT f.friend_uid FROM flag_friend f WHERE f.uid = 37)) 
  OR 
  (users.uid IN (SELECT f.uid FROM flag_friend f WHERE f.friend_uid = 37)) )) 
...

which was generated in D6 by:

...
    else {
      $operator = empty($this->options['not']) ? '=' : '!=';
      $this->query->add_where($group, 'users.uid', "IN (SELECT f.friend_uid FROM {flag_friend} f WHERE f.uid $operator $this->argument)");
      $this->query->add_where($group, 'users.uid', "IN (SELECT f.uid FROM {flag_friend} f WHERE f.friend_uid $operator $this->argument)");
    }

So how do we do this in D7?

sirkitree’s picture

Status: Active » Fixed

Looks like we need to use add_where_expression() instead of add_where().

http://drupalcode.org/project/flag_friend.git/commitdiff/afabdd346f05170...

This may run into problems if users try to implement the OR logic in the newer views ui.

jyee’s picture

Yeah, the second patch i had above ("updated patch") had the add_where_expression(). Sorry, I realize that i wasn't particularly clear about that.

jon pugh’s picture

I had been working on this problem, and a number of other ones, and have rolled them up into one big patch.

see http://drupal.org/node/1101496

Sorry for grouping them up like this, but they are all critical issues and for my current project, which has an extremely limited time budget.

Pretty heavily tested so far.

jon pugh’s picture

Status: Fixed » Needs work

forgot to mark as needs work... this patch still broken for me.

sirkitree’s picture

Status: Fixed » Closed (fixed)

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