From d10e7634550ed4bcffd31d35a56d0c2c77352eed Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Tue, 14 May 2024 10:53:51 +1200 Subject: [PATCH] DOC Document using raw SQL to query join tables (#508) --- en/02_Developer_Guides/00_Model/02_Relations.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/en/02_Developer_Guides/00_Model/02_Relations.md b/en/02_Developer_Guides/00_Model/02_Relations.md index 927367287..42e5a2542 100644 --- a/en/02_Developer_Guides/00_Model/02_Relations.md +++ b/en/02_Developer_Guides/00_Model/02_Relations.md @@ -530,10 +530,20 @@ class TeamSupporter extends DataObject You can filter on the relation by the extra fields automatically, assuming they don't conflict with names of fields on other tables in the query. ```php -$team = Team::get()->byId(1); +$team = Team::get()->byID(1); $supporters = $team->Supporters()->filter(['Ranking' => 1]); ``` +If the field names conflict, you can explicitly query the field from the join record using raw SQL. There are potential security concerns to consider with this approach. See [raw SQL](/developer_guides/model/data_model_and_orm/#raw-sql) for more information. + +```php +use SilverStripe\ORM\DataObject; + +$rankingColumn = DataObject::getSchema()->sqlColumnForField(TeamSupporter::class, 'Ranking'); +$team = Team::get()->byID(1); +$supporters = $team->Supporters()->where([$rankingColumn => 1]); +``` + > [!TIP] > For records accessed in a [`ManyManyThroughList`](api:SilverStripe\ORM\ManyManyThroughList), you can access the join record (e.g. for our example above a `TeamSupporter` instance) by calling [`getJoin()`](api:SilverStripe\ORM\DataObject::getJoin()) or as the `$Join` property in templates.