Skip to content

Commit 5fbfe93

Browse files
committed
Add info about querying relationships
1 parent 954305f commit 5fbfe93

18 files changed

+516
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
To query an inverse relationship in C#, you cannot use LINQ. Instead, pass a
2+
string predicate using RQL. The following example shows how you could find
3+
all Users who have Items that contain the word "oscillator".
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
An application could use the following object schemas to indicate
2+
that a Person may own multiple Dogs by including them in its ``dog``
3+
property:
4+
5+
.. literalinclude:: /examples/generated/dotnet/Relationships.snippet.one-to-many.cs
6+
:language: csharp
7+
8+
To see the to-many relationship of Person to Dog, you query for the
9+
Person and get that person's Dogs.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
To query a direct relationship, you can use LINQ syntax.
2+
See the following example for how to query a one-to-one relationship.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
In Dart, there are two ways to query inverse relationships.
2+
3+
You can use the :flutter-sdk:`getBacklinks()
4+
<realm/RealmObjectBase/getBacklinks.html>` method to retrieve an object at
5+
the origin of the relationship from the related object.
6+
7+
Here, we use the ``getBacklinks()`` method to find any ``User`` objects that
8+
link to the specified tasks through the ``tasks`` backlinks property:
9+
10+
.. literalinclude:: /examples/generated/flutter/backlinks_test.snippet.query-backlink-inverse-relationship.dart
11+
:language: dart
12+
13+
You can also filter by inverse relationships using the
14+
``@links.<Type>.<Property>`` syntax. For example, a filter can match
15+
a ``Task`` object based on properties of the ``User``
16+
object that references it.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Consider the following relationship between classes ``Cat`` and
2+
``Human``. In this example, all cats link to their human (or
3+
multiple humans, if multiple human objects refer to the same cat).
4+
Realm calculates the owners of each cat for you based on the field
5+
name you provide to the ``@LinkingObjects`` annotation:
6+
7+
.. include:: /examples/generated/java/sync/Cat.snippet.complete.java.rst
8+
9+
.. include:: /examples/generated/java/sync/Human.snippet.complete.java.rst
10+
11+
To query this relationship, use dot notation in a
12+
:ref:`query <sdks-java-filter-data>` to access any property
13+
of the linked object.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Consider the following relationship between classes ``Dog`` and
2+
``Person``. In this example, all dogs link to their owner (or
3+
multiple owners, if multiple person objects refer to the same dog).
4+
Realm calculates the owners of each dog for you based on the field
5+
name you provide to the ``@LinkingObjects`` annotation:
6+
7+
.. include:: /examples/generated/java/sync/Dog.snippet.complete.kt.rst
8+
9+
.. include:: /examples/generated/java/sync/Person.snippet.complete.kt.rst
10+
11+
To query this relationship, use dot notation in a
12+
:ref:`query <sdks-java-filter-data>` to access any property
13+
of the linked object.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Consider the following relationship between classes ``Human`` and
2+
``Cat``. This arrangement allows each human to own a single cat:
3+
4+
.. literalinclude:: /examples/generated/java/sync/Human.snippet.complete.java
5+
:language: java
6+
:emphasize-lines: 12
7+
:copyable: false
8+
9+
.. literalinclude:: /examples/generated/java/sync/Cat.snippet.complete.java
10+
:language: java
11+
:copyable: false
12+
13+
To query this relationship, use dot notation in a :ref:`query
14+
<sdks-java-filter-data>` to access any property of the linked object.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Consider the following relationship between classes ``Person`` and
2+
``Dog``. This arrangement allows each person to own a single dog:
3+
4+
.. literalinclude:: /examples/generated/java/sync/Person.snippet.complete.kt
5+
:language: kotlin
6+
:emphasize-lines: 10
7+
:copyable: false
8+
9+
.. literalinclude:: /examples/generated/java/sync/Dog.snippet.complete.kt
10+
:language: kotlin
11+
:copyable: false
12+
13+
To query this relationship, use dot notation in a :ref:`query
14+
<sdks-java-filter-data>` to access any propertyof the linked object:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
You can dynamically retrieve an object with an inverse relationship without
2+
defining a ``linkingObjects`` type in its schema. When you need to retrieve
3+
the linked object, call the :js-sdk:`Realm.Object.linkingObjects()
4+
<classes/Object.html#linkingObjects>` query.
5+
6+
The ``.linkingObjects()`` method returns a :ref:`Results collection
7+
<sdks-read-access-results>` of objects whose property inverts the relationship.
8+
9+
In this example, only one manufacturer makes the Sentra car model, so we
10+
can expect that manufacturer to be named Nissan.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
In the following example, a parent object of type ``User`` has an inverse
2+
relationship to a child object of type ``Post``. We can query the parent
3+
object's ``User.posts`` relationship ("User has many Posts") as well
4+
as the inverse ``Post.user`` relationship ("Post belongs to User"):
5+
6+
.. literalinclude:: /examples/generated/kotlin/ReadTest.snippet.query-inverse-relationship.kt
7+
:language: kotlin
8+
9+
.. important:: Querying Inverse Relationship by Remapped Class Names
10+
11+
If the inverse relationship property is an object type with a
12+
remapped (persisted) class name, you *must* use the remapped class
13+
name in the raw RQL query.

0 commit comments

Comments
 (0)