The current views integration added in #965860: Add views integration doesn't allow to perform reverse joins.

In Profile2's case (#1047904: Support views relationship "user -> profile"), there is only a views relationship from the profile type to the user, but not the other way around.

Attached patch attempts to introduce the reversed relationships for each entity.

Doesn't fully work correctly yet, help appreciated.

Comments

Status: Needs review » Needs work

The last submitted patch, entity.views-relations-reversed.0.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
StatusFileSize
new1.68 KB

And this one works. :)

sun’s picture

StatusFileSize
new3.8 KB

And now for real.

johnv’s picture

(How) does this patch influence/interact with the following patch which enables reverse relationships in Views itself?
Http://drupal.org/node/1043260

sun’s picture

Thanks for pointing to #1043260: Reverse relationship -- however, I'm not sure whether a completely generic approach within Views itself is going to work out. Obviously, it would make this entire code and patch unnecessary, but as of now, I doubt that Views will be able to add built-in support anytime soon.

fago’s picture

StatusFileSize
new2.47 KB

Great idea!

-        return array(
+        $data += array(

This changes are confusing me, as still $data is written only once? So I don't think we need to do that.

   public function __construct($type) {
     $this->type = $type;
     $this->info = entity_get_info($type);
+    $this->relationships = array();
   }

Hm, so if the views data would be built multiple times on a page request, the controller would add in the same relationship multiple times? I've just moved that over to views_data().

I've tried to come up with an auto-generated help text for the reverse relationship and updated the patch. Patch works for me, however perhaps we should do something like entity_lcfirst() which works like lcfirst() in PHP 5.3? I miss drupal_lcfirst().

fago’s picture

StatusFileSize
new2.7 KB

Correct patch.

roam2345’s picture

Been testing this out as I need the reverse relation ship with something im doing with profile2.

Once applied the patch I keep getting.

Debug:
'Exception: SQLSTATE[23000]: Integrity constraint violation: 1052 Column \'uid\' in field list is ambiguous'
in views_plugin_query_default->execute() (line 1314 of /var/www-sites/drupal/dev/sites/all/modules/views/plugins/views_plugin_query_default.inc).
sun’s picture

@jucallme: Odd - didn't get this error with my patch. Didn't test @fago's patch yet. I'm not able to see an essential difference between the two patches, but I'm pretty sure that I should've gotten that error as well, since I also tested with profile2 and 'uid' exists on both tables. Hence, something must be different.

roam2345’s picture

@sun yeah I need to give it another once over to figure out exactly what went wrong.. I created a patch for profile2 to do these views relationships there. Fargo pointed me here, so I removed my patched version of profile2 and patched entity API (dev) with this. Re configured my views flush all the caches and it kept spazzing out with that error, used fargo's patch.

I had to release with my patched version of profile2, but should have time 2m / Monday to get down and dirty with this as it's the way forward. So I'll get better feed back then..

Side note, strange that my watch list not have this issue in it.. Must be something changed since git down time.

roam2345’s picture

As promised, retested this.

The reason I was getting

Debug:
'Exception: SQLSTATE[23000]: Integrity constraint violation: 1052 Column \'uid\' in field list is ambiguous'
in views_plugin_query_default->execute() (line 1314 of /var/www-sites/drupal/dev/sites/all/modules/views/plugins/views_plugin_query_default.inc).

Was due to the fact that I did not update the UID field in my out put of the view. It was still using the old relation ship from the patched version of my profile2.

All is good use this quite extensively building some hefty views, and its green lights all round. http://www.astuteresourcing.com/ currently has this patch applied and we use it for all the profile 2 entities we have defined.

Thanks, @sun and @fargo.

fago’s picture

Sounds good. @sun: Does the patch in #7 work for you too?

sun’s picture

Status: Needs review » Reviewed & tested by the community

Didn't test and won't be able to test soon, but I also don't see incompatible differences, so let's proceed.

roam2345’s picture

Sorry but further testing my side im getting into this case

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 'LIKE 'applicant' ESCAPE '\\') ))) subquery' at line 5: SELECT COUNT(*) AS expression FROM (SELECT 1 AS expression FROM {profile} profile INNER JOIN {users} users_profile ON profile.uid = users_profile.uid INNER JOIN {flag_content} flag_content_users ON users_profile.uid = flag_content_users.content_id AND flag_content_users.fid = :views_join_condition_0 LEFT JOIN {field_data_field_applicant_first_name} field_data_field_applicant_first_name ON profile.pid = field_data_field_applicant_first_name.entity_id AND (field_data_field_applicant_first_name.entity_type = :views_join_condition_1 AND field_data_field_applicant_first_name.deleted = :views_join_condition_2) LEFT JOIN {field_data_field_applicant_last_name} field_data_field_applicant_last_name ON profile.pid = field_data_field_applicant_last_name.entity_id AND (field_data_field_applicant_last_name.entity_type = :views_join_condition_3 AND field_data_field_applicant_last_name.deleted = :views_join_condition_4) WHERE (( (.type LIKE :db_condition_placeholder_0 ESCAPE '\\') ))) subquery; Array ( [:db_condition_placeholder_0] => applicant [:views_join_condition_0] => 3 [:views_join_condition_1] => profile2 [:views_join_condition_2] => 0 [:views_join_condition_3] => profile2 [:views_join_condition_4] => 0 ) in views_plugin_pager->execute_count_query() (line 141 of /usr/www/users/astuten/sites/all/modules/views/plugins/views_plugin_pager.inc).

This is only happening when I add a field filter of profile2 type... looks like there is an escape char sneaking its way in.

Not sure if this is a views issue or ours, as it works in the preview. But as soon as I save the view it throws this.

sun’s picture

The actual error is in .type:

WHERE (( (.type LIKE :db_condition_placeholder_0 ...

You might have to specify the relation to use for the profile type condition.

Shadlington’s picture

Subscribe

fago’s picture

Status: Reviewed & tested by the community » Fixed

Committed, thanks.

Status: Fixed » Closed (fixed)

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

steinmb’s picture

Status: Closed (fixed) » Needs work

Not entire sure this is 100% fixed, still have problems with profile2. The page is a simple view (basetable, content) showing just a few fields of a news article, so nothing fancy. I'm sorry if I'm barking up the wrong tree, it might be a profile2 or views issue..

Setup:
Entity API 7.x-1.x-dev (2011-mar-22)
ctools 7.x-1.0-alpha4
profile2 7.x-1.0-beta2
Views 7.x-3.0-beta3

Debug:
'Exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column \'profile.pid\' in \'field list\''
in views_plugin_query_default->execute() (line 1354 of /drupal/sites/all/modules/views/plugins/views_plugin_query_default.inc).

Not really sure how this all works, but I'm guessing that profile2 build a new enity but some how this fail. I looked on the query generated by views

SELECT 
  node.title AS node_title, 
  node.nid AS nid, 
  node.language AS node_language, 
  node.created AS node_created, 
  profile.pid AS profile_pid, 
  'profile2' AS field_data_field_bilete_profile2_entity_type, 
  'node' AS field_data_field_ingress_node_entity_type
{node} node
WHERE (( (node.type IN  ('nyhende')) ))
LIMIT 20 OFFSET 0

Tried to run this manually against the db. and it is correct, this does not work, "profile.pid AS profile_pid"

fago’s picture

Status: Needs work » Closed (fixed)

>'Exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column \'profile.pid\' in \'field list\''

Sounds like your mysql table is missing the column. Anyway, please open new issues for any general views integration troubles.

bryancasler’s picture

subscribe

dargno’s picture

subscribing

Got the same SQL error as above;

Can we just add the column manually to fix the issue?

d.holmen@gmail.com’s picture

Subscribing

SeanBot2000’s picture

Subscribing

I am also getting this error trying to expose profile data in a view.

smk-ka’s picture

Status: Closed (fixed) » Needs work
+++ views/entity.views.inc
@@ -98,6 +109,23 @@ class EntityDefaultViewsController {
+      $this->relationships[$info['base table']][$this->info['base table']] = array(
+        'title' => $this->info['label'],
+        'help' => t("Associated @label via the @label's @property.", array('@label' => $label_lowercase, '@property' => $property_label_lowercase)),
+        'relationship' => array(
+          'label' => $this->info['label'],
+          'handler' => 'views_handler_relationship',
+          'base' => $this->info['base table'],
+          'base field' => $views_field_name,
+          'relationship field' => isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'],
+        ),
+      );

This isn't sufficient: if you have more than one entity property that links to another entity of the same type (i.e., same $info['base table']), you're overwriting any previously set relationships. $this->relationships needs to take the current field into account, and pass that information up to entity_views_data().

smk-ka’s picture

Status: Needs work » Needs review
StatusFileSize
new867 bytes

The fix is simple, but requires updating all views...

crifi’s picture

+1

fago’s picture

ouch. People won't be happy with that, but we need to fix this. -> committed.

fago’s picture

Status: Needs review » Fixed
carnity’s picture

Status: Fixed » Needs review
crifi’s picture

Status: Needs review » Fixed

Issue was fixed and committed, see #28!

Status: Fixed » Closed (fixed)

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

fago’s picture

Status: Closed (fixed) » Needs work

ouch, #26 is going to break everyone's og views ... We should re-evaluate it.

Proposal: Keep the name of the first reverse-relationship to the old name, re-name all others.

fago’s picture

Status: Needs work » Fixed

ok, I've done so and committed it. See http://drupalcode.org/project/entity.git/commit/f0728d1

This means, user of the dev version might have to update their relationships *again* - sry for that. However, the majority of users which still uses the latest beta release won't get broken handlers that way.

steinmb’s picture

OK, thanks for all your hard work. I think most people are going to be very thankful for a patch that do not break every view on their site :)

Status: Fixed » Closed (fixed)

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