Answers.Body: <p>Not sure if this is what you're after but works for me; the 'join on' is a bit hidden in the 'Select' box.</p>
  
  <pre><code>CRM.api4('Relationship', 'get', {
    select: ["contact_a.display_name"],
    where: [["relationship_type_id", "=", 5]],
    limit: 25
  }).then(function(relationships) {
    // do something with relationships array
  }, function(failure) {
    // handle failure
  })
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-11T18:34:51.540Z
Answers.LastActivityDate: 2019-11-12T23:17:26.427Z
Answers.LastEditDate: 2019-11-12T23:17:26.427Z
Answers.UserDisplayName: RebeccaTregenna
Answers.Body: <p>For the part about making your first-step example work, use 'contact_a.display_name' instead of display_name.</p>
  
  <p>Also it's missing from your example but it would be <code>'Relationship', 'get',</code> at the very beginning, so the whole thing, to also get the org, is</p>
  
  <pre><code>$relationships = civicrm_api4('Relationship', 'get', [
    'select' =&gt; [
      'contact_a.display_name', 'contact_b.display_name'
    ],
    'where' =&gt; [
      ['relationship_type_id', '=', 15],
    ],
    'checkPermissions' =&gt; false,
  ]);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-13T14:43:10.833Z
Answers.Text: So are you saying I MUST do relationships from the relationships entity? Can't I start from contact, add relationship, add the related contact?
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-13T14:43:56.907Z
Answers.Text: I find the api is always the reverse for joins. You kind of work backwards. See also https://civicrm.stackexchange.com/questions/28126/fetch-membership-type-along-with-the-contact-details-with-civicrm-api/28131#28131. You could start from contact and write a procedural loop, but then it's not really a join.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-13T15:22:56.537Z
Answers.Text: Ok, so it it basically the case that APIv4 can do joins but only joins that are 1 deep? i.e. I can go Relationship to two contacts (and a relationship type); but I can't go contact → relationship → contact, because that's 2 deep. Or, for example, I couldn't get the phone number of the contact in your example (because that's relationship → contact → phone)?
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-13T15:55:40.177Z
Answers.Text: I don't know the answer. Maybe there's a syntax for it. Or maybe with chaining, but then I get the impression from the docs that chaining isn't saving db calls vs doing a loop, just two or three less lines of code than writing your own loop.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-11T18:26:36.893Z
Answers.LastActivityDate: 2019-11-11T18:26:36.893Z
Answers.UserDisplayName: Demerit
Body: <p><em>Wilma</em> has  relationship: <em>is campaigner at</em> organisation <em>System Change Not Charity</em>.</p>
  
  <p>How can I get a list of individuals like Wilma and the organisations that they are campaigners for?</p>
  
  <p>I've read the <a href="https://docs.civicrm.org/dev/en/latest/api/v4/joins/" rel="nofollow noreferrer">documentation</a> but I can't figure out how I would join on relationship?</p>
  
  <p>I tried this which I thought would at least give me the names of people with that campaigner relationship type, but it gives nothing.</p>
  
  <ul>
  <li>Entity: Contact </li>
  <li>Action: get</li>
  </ul>
  
  
  
  <pre><code>{                                                                                                                                                                                           
      "select": ["display_name"],                                                                                                                                                               
      "where": [                                                                                                                                                                                                                                                                                                                                                        
        ["relationships.relationship_type_id", "=", 15]                                                                                                                                          
      ],                                                                                                                                                                                        
      "limit": 25,                                                                                                                                                                              
      "checkPermissions": false                                                                                                                                                                 
  }
  </code></pre>
  
Tags: <api>
Title: How to get data via relationships with APIv4?
ThreadText: # How to get data via relationships with APIv4?
  
  (Asked by: username_0 on 2019-11-11)
  
  *Wilma* has relationship: *is campaigner at* organisation *System Change Not Charity*.
  
  How can I get a list of individuals like Wilma and the organisations that they are campaigners for?
  
  I've read the [documentation](https://docs.civicrm.org/dev/en/latest/api/v4/joins/) but I can't figure out how I would join on relationship?
  
  I tried this which I thought would at least give me the names of people with that campaigner relationship type, but it gives nothing.
  
  * Entity: Contact
  * Action: get
  
  ```
  {                                                                                                                                                                                           
      "select": ["display_name"],                                                                                                                                                               
      "where": [                                                                                                                                                                                                                                                                                                                                                        
        ["relationships.relationship_type_id", "=", 15]                                                                                                                                          
      ],                                                                                                                                                                                        
      "limit": 25,                                                                                                                                                                              
      "checkPermissions": false                                                                                                                                                                 
  }
  
  ```
  
  **Tags:** api
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-11-11. Score: 2
  
  Not sure if this is what you're after but works for me; the 'join on' is a bit hidden in the 'Select' box.
  
  ```
  CRM.api4('Relationship', 'get', {
    select: ["contact_a.display_name"],
    where: [["relationship_type_id", "=", 5]],
    limit: 25
  }).then(function(relationships) {
    // do something with relationships array
  }, function(failure) {
    // handle failure
  })
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2019-11-11. Score: 1
  
  For the part about making your first-step example work, use 'contact\_a.display\_name' instead of display\_name.
  
  Also it's missing from your example but it would be `'Relationship', 'get',` at the very beginning, so the whole thing, to also get the org, is
  
  ```
  $relationships = civicrm_api4('Relationship', 'get', [
    'select' => [
      'contact_a.display_name', 'contact_b.display_name'
    ],
    'where' => [
      ['relationship_type_id', '=', 15],
    ],
    'checkPermissions' => false,
  ]);
  
  ```
  
  > **Comments:**
  > **username_0**: So are you saying I MUST do relationships from the relationships entity? Can't I start from contact, add relationship, add the related contact?  
  > **username_2**: I find the api is always the reverse for joins. You kind of work backwards. See also https://civicrm.stackexchange.com/questions/28126/fetch-membership-type-along-with-the-contact-details-with-civicrm-api/28131#28131. You could start from contact and write a procedural loop, but then it's not really a join.  
  > **username_0**: Ok, so it it basically the case that APIv4 can do joins but only joins that are 1 deep? i.e. I can go Relationship to two contacts (and a relationship type); but I can't go contact → relationship → contact, because that's 2 deep. Or, for example, I couldn't get the phone number of the contact in your example (because that's relationship → contact → phone)?  
  > **username_2**: I don't know the answer. Maybe there's a syntax for it. Or maybe with chaining, but then I get the impression from the docs that chaining isn't saving db calls vs doing a loop, just two or three less lines of code than writing your own loop.  
  
  ---
  
  