Skip to content

Commit

Permalink
Add info about querying relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
dacharyc committed Aug 9, 2024
1 parent 954305f commit 5fbfe93
Show file tree
Hide file tree
Showing 18 changed files with 516 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To query an inverse relationship in C#, you cannot use LINQ. Instead, pass a
string predicate using RQL. The following example shows how you could find
all Users who have Items that contain the word "oscillator".
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
An application could use the following object schemas to indicate
that a Person may own multiple Dogs by including them in its ``dog``
property:

.. literalinclude:: /examples/generated/dotnet/Relationships.snippet.one-to-many.cs
:language: csharp

To see the to-many relationship of Person to Dog, you query for the
Person and get that person's Dogs.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
To query a direct relationship, you can use LINQ syntax.
See the following example for how to query a one-to-one relationship.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
In Dart, there are two ways to query inverse relationships.

You can use the :flutter-sdk:`getBacklinks()
<realm/RealmObjectBase/getBacklinks.html>` method to retrieve an object at
the origin of the relationship from the related object.

Here, we use the ``getBacklinks()`` method to find any ``User`` objects that
link to the specified tasks through the ``tasks`` backlinks property:

.. literalinclude:: /examples/generated/flutter/backlinks_test.snippet.query-backlink-inverse-relationship.dart
:language: dart

You can also filter by inverse relationships using the
``@links.<Type>.<Property>`` syntax. For example, a filter can match
a ``Task`` object based on properties of the ``User``
object that references it.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Consider the following relationship between classes ``Cat`` and
``Human``. In this example, all cats link to their human (or
multiple humans, if multiple human objects refer to the same cat).
Realm calculates the owners of each cat for you based on the field
name you provide to the ``@LinkingObjects`` annotation:

.. include:: /examples/generated/java/sync/Cat.snippet.complete.java.rst

.. include:: /examples/generated/java/sync/Human.snippet.complete.java.rst

To query this relationship, use dot notation in a
:ref:`query <sdks-java-filter-data>` to access any property
of the linked object.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Consider the following relationship between classes ``Dog`` and
``Person``. In this example, all dogs link to their owner (or
multiple owners, if multiple person objects refer to the same dog).
Realm calculates the owners of each dog for you based on the field
name you provide to the ``@LinkingObjects`` annotation:

.. include:: /examples/generated/java/sync/Dog.snippet.complete.kt.rst

.. include:: /examples/generated/java/sync/Person.snippet.complete.kt.rst

To query this relationship, use dot notation in a
:ref:`query <sdks-java-filter-data>` to access any property
of the linked object.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Consider the following relationship between classes ``Human`` and
``Cat``. This arrangement allows each human to own a single cat:

.. literalinclude:: /examples/generated/java/sync/Human.snippet.complete.java
:language: java
:emphasize-lines: 12
:copyable: false

.. literalinclude:: /examples/generated/java/sync/Cat.snippet.complete.java
:language: java
:copyable: false

To query this relationship, use dot notation in a :ref:`query
<sdks-java-filter-data>` to access any property of the linked object.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Consider the following relationship between classes ``Person`` and
``Dog``. This arrangement allows each person to own a single dog:

.. literalinclude:: /examples/generated/java/sync/Person.snippet.complete.kt
:language: kotlin
:emphasize-lines: 10
:copyable: false

.. literalinclude:: /examples/generated/java/sync/Dog.snippet.complete.kt
:language: kotlin
:copyable: false

To query this relationship, use dot notation in a :ref:`query
<sdks-java-filter-data>` to access any propertyof the linked object:
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
You can dynamically retrieve an object with an inverse relationship without
defining a ``linkingObjects`` type in its schema. When you need to retrieve
the linked object, call the :js-sdk:`Realm.Object.linkingObjects()
<classes/Object.html#linkingObjects>` query.

The ``.linkingObjects()`` method returns a :ref:`Results collection
<sdks-read-access-results>` of objects whose property inverts the relationship.

In this example, only one manufacturer makes the Sentra car model, so we
can expect that manufacturer to be named Nissan.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
In the following example, a parent object of type ``User`` has an inverse
relationship to a child object of type ``Post``. We can query the parent
object's ``User.posts`` relationship ("User has many Posts") as well
as the inverse ``Post.user`` relationship ("Post belongs to User"):

.. literalinclude:: /examples/generated/kotlin/ReadTest.snippet.query-inverse-relationship.kt
:language: kotlin

.. important:: Querying Inverse Relationship by Remapped Class Names

If the inverse relationship property is an object type with a
remapped (persisted) class name, you *must* use the remapped class
name in the raw RQL query.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In the following example, the ``Forest`` object type has a property called
``nearbyPonds`` that is a ``RealmList`` of type ``Pond``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In the following example, the ``Frog`` object type has a property called
``favoritePond`` of type ``Pond``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
You can query :swift-sdk:`RLMLinkingObjects
<Classes.html#/c:objc(cs)RLMLinkingObjects>`, an auto-updating container type,
to access zero or more objects that are linked to its owning model object
through a property relationship. You can query ``RLMLinkingObjects`` with the
same APIs and predicates as
:swift-sdk:`RLMArray\<Element\> <Classes/RLMArray.html>` and
:swift-sdk:`RLMResults\<Element\> <Classes/RLMResults.html>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
You can query :swift-sdk:`LinkingObjects <Structs/LinkingObjects.html>`,
an auto-updating container type, to access zero or more objects that are
linked to its owning model object through a property relationship. You can
query ``LinkingObjects`` with the same APIs and predicates as
:swift-sdk:`List\<Element\> <Classes/List.html>` and
:swift-sdk:`Results\<Element\> <Structs/Results.html>`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
.. tabs-drivers::

tabs:
- id: cpp-sdk
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.cpp
:language: cpp
:copyable: false

- id: csharp
content: |

.. literalinclude:: /examples/generated/dotnet/Relationships.snippet.inverse-query.cs
:language: csharp

- id: dart
content: |

.. literalinclude:: /examples/generated/flutter/backlinks_test.snippet.filter-backlinks-rql.dart
:language: dart

- id: java
content: |

.. include:: /examples/generated/java/sync/RealmQueryTest.snippet.query-an-inverse-relationship.java.rst

- id: java-kotlin
content: |

.. include:: /examples/generated/java/sync/RealmQueryTest.snippet.query-an-inverse-relationship.kt.rst

- id: javascript
content: |

.. literalinclude:: /examples/generated/node/v12/relationships.test.snippet.obtain-inverse-relationship-dynamically.js
:language: javascript

- id: kotlin
content: |

.. literalinclude:: /examples/generated/kotlin/ReadTest.snippet.persisted-name-inverse.kt
:language: kotlin
:emphasize-lines: 1

.. literalinclude:: /examples/generated/kotlin/ReadTest.snippet.query-inverse-persisted-name.kt
:language: kotlin
:emphasize-lines: 4

- id: objectivec
content: |

.. literalinclude:: /examples/generated/code/start/ReadWriteData.snippet.query-an-inverse-relationship.m
:language: objectivec

- id: swift
content: |

.. literalinclude:: /examples/generated/code/start/ReadRealmObjects.snippet.query-an-inverse-relationship.swift
:language: swift

- id: typescript
content: |

.. literalinclude:: /examples/generated/node/v12/relationships.test.snippet.obtain-inverse-relationship-dynamically.ts
:language: typescript
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
.. tabs-drivers::

tabs:
- id: cpp-sdk
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.cpp
:language: cpp
:copyable: false

- id: csharp
content: |

.. literalinclude:: /examples/generated/dotnet/Relationships.snippet.one-to-many-query.cs
:language: csharp

- id: dart
content: |

.. literalinclude:: /examples/generated/flutter/data_types_test.snippet.realmlist-use.dart
:language: dart

- id: java
content: |

.. literalinclude:: /examples/generated/java/sync/RealmQueryTest.snippet.query-a-relationship.java
:language: java
:emphasize-lines: 16

- id: java-kotlin
content: |

.. literalinclude:: /examples/generated/java/sync/RealmQueryTest.snippet.query-a-relationship.kt
:language: kotlin
:emphasize-lines: 15

- id: javascript
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.js
:language: javascript
:copyable: false

- id: kotlin
content: |

.. literalinclude:: /examples/generated/kotlin/ReadTest.snippet.query-to-many-relationship.kt
:language: kotlin

- id: objectivec
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.m
:language: objectivec
:copyable: false

- id: swift
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.swift
:language: swift
:copyable: false

- id: typescript
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.ts
:language: typescript
:copyable: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
.. tabs-drivers::

tabs:
- id: cpp-sdk
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.cpp
:language: cpp
:copyable: false

- id: csharp
content: |

.. literalinclude:: /examples/generated/dotnet/Relationships.snippet.one-to-one-query.cs
:language: csharp

- id: dart
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.dart
:language: dart
:copyable: false

- id: java
content: |

.. literalinclude:: /examples/generated/java/sync/RealmQueryTest.snippet.query-a-relationship.java
:language: java
:emphasize-lines: 16

- id: java-kotlin
content: |

.. literalinclude:: /examples/generated/java/sync/RealmQueryTest.snippet.query-a-relationship.kt
:language: kotlin
:emphasize-lines: 15

- id: javascript
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.js
:language: javascript
:copyable: false

- id: kotlin
content: |

.. literalinclude:: /examples/generated/kotlin/ReadTest.snippet.query-to-one-relationship.kt
:language: kotlin

- id: objectivec
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.m
:language: objectivec
:copyable: false

- id: swift
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.swift
:language: swift
:copyable: false

- id: typescript
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.ts
:language: typescript
:copyable: false
Loading

0 comments on commit 5fbfe93

Please sign in to comment.