Skip to content

Instantly share code, notes, and snippets.

View GuySartorelli's full-sized avatar

Guy Sartorelli GuySartorelli

View GitHub Profile
@NightJar
NightJar / beMoreEfficient.php
Created May 16, 2024 11:09
Stop doing 1+N queries when using summary fields with relations (e.g. GridFieldDataColumns or GridFieldExportButton)
<?php
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Queries\SQLSelect;
function alterListToSummaryFields(DataList $list): SQLSelect
{
$model = $list->dataClass();
$instance = $model::create();
@NightJar
NightJar / Silverstripe-queries.md
Created February 4, 2020 20:42
Silverstripe & SQL

Why don't Silverstripe CMS queries run in MySQL?

When debugging an ORM call, you may find yourself collecting the SQL that will be exectued via the DataList::sql function - this is great, but when copied and pasted into e.g. PHPMyAdmin, MySQL Workbench, HeidiSQL, DBeaver, Sequel Pro, etc... it doesn't seem to work!

Lets look at an example query that might* be used for a template call such as <% if $Menu(1) %>:

SELECT DISTINCT count(DISTINCT "SiteTree_Live"."ID") AS "Count"
 FROM "SiteTree_Live"
 WHERE (("SiteTree_Live"."ParentID" = 0))