Last active
January 5, 2017 15:44
-
-
Save markguinn/1d900761d88e06aa1e24 to your computer and use it in GitHub Desktop.
Create MySQL view for multi-table Silverstripe DataObject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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