Skip to content

Commit

Permalink
DOC Document using raw SQL to query join tables
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed May 10, 2024
1 parent 6182773 commit ec14520
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions en/02_Developer_Guides/00_Model/02_Relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,16 @@ $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.
Expand Down

0 comments on commit ec14520

Please sign in to comment.