Skip to content

Instantly share code, notes, and snippets.

@markguinn
Last active January 5, 2017 15:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markguinn/1d900761d88e06aa1e24 to your computer and use it in GitHub Desktop.
Save markguinn/1d900761d88e06aa1e24 to your computer and use it in GitHub Desktop.
Create MySQL view for multi-table Silverstripe DataObject
<?php
/**
* Creates a Flat table view for a given class
* @author Mark Guinn <mark@adaircreative.com>
* @date 06.13.2014
*/
class CreateViewForClass extends BuildTask
{
protected $title = 'DEV: Flat View for Class';
protected $description = 'Use class=XXXX to create a flat view of all the tables that make up a given class';
public function run($request) {
if (!Director::isDev()) die("Never run this on a live site.\n");
if (!$request->getVar('class')) die("Give me a class (e.g. framework/sake dev/tasks/CreateViewForClass class=Page)\n");
$class = $request->getVar('class');
$list = DataObject::get($class);
$sql = $list->dataQuery()->sql();
$sql = "create or replace\ndefiner=current_user\nsql security invoker\nview Flat_$class as\n" . $sql;
echo "\nSQL FOR VIEW:\n$sql\n\n";
DB::query($sql);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment