Skip to content

Instantly share code, notes, and snippets.

@lorenzo
Created September 22, 2012 14:46
Show Gist options
  • Save lorenzo/3766399 to your computer and use it in GitHub Desktop.
Save lorenzo/3766399 to your computer and use it in GitHub Desktop.
<?php
class ParentFixture extends CakeTestFixture {
public $import = array();
protected $hasData = false;
protected static $truncating = false;
protected static $log = array();
public function insert($db) {
self::$truncating = false;
if ($this->hasData) {
return;
}
parent::insert($db);
$this->hasData = true;
}
public function truncate($db) {
if (!self::$truncating) {
self::$log = $db->getLog(false, true);
self::$truncating = true;
}
foreach (self::$log['log'] as $i => $q) {
if (preg_match('/^UPDATE|^INSERT/i', $q['query'])) {
if (strpos($q['query'], $db->fullTableName($this->table, false, false)) !== false) {
parent::truncate($db);
$this->hasData = false;
return;
}
} else {
unset(self::$log['log'][$i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment