lib/db/cortex.php
static public function setdown($db=null, $table=null) {
		$self = get_called_class();
		if (is_null($db) || is_null($table))
			$df = $self::resolveConfiguration();
		if (!is_object($db=(is_string($db=($db?:$df['db']))?\Base::instance()->get($db):$db)))
			trigger_error(self::E_CONNECTION,E_USER_ERROR);
		if (strlen($table=strtolower($table?:$df['table']))==0)
			trigger_error(self::E_NO_TABLE,E_USER_ERROR);
		if (isset($df) && !empty($df['fieldConf']))
			$fields = $df['fieldConf'];
		else
			$fields = array();
		$deletable = array();
		$deletable[] = $table;
		foreach ($fields as $key => $field) {
			$field = static::resolveRelationConf($field);
			if (array_key_exists('has-many',$field)) {
				if (!is_array($relConf = $field['has-many']))
					continue;
				$rel = $relConf[0]::resolveConfiguration();
				// check if foreign conf matches m:m
				if (array_key_exists($relConf[1],$rel['fieldConf']) && !is_null($relConf[1])
					&& key($rel['fieldConf'][$relConf[1]]) == 'has-many') {
					// compute mm table name
					$deletable[] = isset($relConf[2]) ? $relConf[2] :
						static::getMMTableName(
							$rel['table'], $relConf[1], $table, $key,
							$rel['fieldConf'][$relConf[1]]['has-many']);
				}
			}
		}

		if($db instanceof Jig) {
			/** @var Jig $db */
			$dir = $db->dir();
			foreach ($deletable as $item)
				if(file_exists($dir.$item))
					unlink($dir.$item);
		} elseif($db instanceof SQL) {
			/** @var SQL $db */
			$schema = new Schema($db);
			$tables = $schema->getTables();
			foreach ($deletable as $item)
				if(in_array($item, $tables))
					$schema->dropTable($item);
		} elseif($db instanceof Mongo) {
			/** @var Mongo $db */
			foreach ($deletable as $item)
				$db->selectCollection($item)->drop();
		}
	}
static public function setdown($db=null, $table=null) {
		$self = get_called_class();
		if (is_null($db) || is_null($table))
			$df = $self::resolveConfiguration();
		if (!is_object($db=(is_string($db=($db?:$df['db']))?\Base::instance()->get($db):$db)))
			trigger_error(self::E_CONNECTION,E_USER_ERROR);
		if (strlen($table=strtolower($table?:$df['table']))==0)
			trigger_error(self::E_NO_TABLE,E_USER_ERROR);
		if (isset($df) && !empty($df['fieldConf']))
			$fields = $df['fieldConf'];
		else
			$fields = array();
		$deletable = array();
		$deletable[] = $table;
		foreach ($fields as $key => $field) {
			$field = static::resolveRelationConf($field);
			if (array_key_exists('has-many',$field)) {
				if (!is_array($relConf = $field['has-many']))
					continue;
				$rel = $relConf[0]::resolveConfiguration();
				// check if foreign conf matches m:m
				if (array_key_exists($relConf[1],$rel['fieldConf']) && !is_null($relConf[1])
					&& key($rel['fieldConf'][$relConf[1]]) == 'has-many') {
					// compute mm table name
					$deletable[] = isset($relConf[2]) ? $relConf[2] :
						static::getMMTableName(
							$rel['table'], $relConf[1], $table, $key,
							$rel['fieldConf'][$relConf[1]]['has-many']);
				}
			}
		}

		if($db instanceof Jig) {
			/** @var Jig $db */
			$dir = $db->dir();
			foreach ($deletable as $item)
				if(file_exists($dir.$item))
					unlink($dir.$item);
		} elseif($db instanceof SQL) {
			/** @var SQL $db */
			$schema = new Schema($db);
			$tables = $schema->getTables();
			foreach ($deletable as $item)
				if(in_array($item, $tables))
					$schema->dropTable($item);
		} elseif($db instanceof Mongo) {
			/** @var Mongo $db */
			foreach ($deletable as $item)
				$db->selectCollection($item)->drop();
		}
	}
static public function setdown ( $ db = null , $ table = null ) { $ self = get_called_class ( ) ; if ( is_null ( $ db ) || is_null ( $ table ) ) $ df = $ self :: resolveConfiguration ( ) ; if ( ! is_object ( $ db = ( is_string ( $ db = ( $ db ? : $ df [ 'db' ] ) ) ? \ Base :: instance ( ) -> get ( $ db ) : $ db ) ) ) trigger_error ( self :: E_CONNECTION , E_USER_ERROR ) ; if ( strlen ( $ table = strtolower ( $ table ? : $ df [ 'table' ] ) ) == 0 ) trigger_error ( self :: E_NO_TABLE , E_USER_ERROR ) ; if ( isset ( $ df ) && ! empty ( $ df [ 'fieldConf' ] ) ) $ fields = $ df [ 'fieldConf' ] ; else $ fields = array ( ) ; $ deletable = array ( ) ; $ deletable [ ] = $ table ; foreach ( $ fields as $ key => $ field ) { $ field = static :: resolveRelationConf ( $ field ) ; if ( array_key_exists ( 'has-many' , $ field ) ) { if ( ! is_array ( $ relConf = $ field [ 'has-many' ] ) ) continue ; $ rel = $ relConf [ 0 ] :: resolveConfiguration ( ) ; // check if foreign conf matches m:m if ( array_key_exists ( $ relConf [ 1 ] , $ rel [ 'fieldConf' ] ) && ! is_null ( $ relConf [ 1 ] ) && key ( $ rel [ 'fieldConf' ] [ $ relConf [ 1 ] ] ) == 'has-many' ) { // compute mm table name $ deletable [ ] = isset ( $ relConf [ 2 ] ) ? $ relConf [ 2 ] : static :: getMMTableName ( $ rel [ 'table' ] , $ relConf [ 1 ] , $ table , $ key , $ rel [ 'fieldConf' ] [ $ relConf [ 1 ] ] [ 'has-many' ] ) ; } } } if ( $ db instanceof Jig ) { /** @var Jig $db */ $ dir = $ db -> dir ( ) ; foreach ( $ deletable as $ item ) if ( file_exists ( $ dir . $ item ) ) unlink ( $ dir . $ item ) ; } elseif ( $ db instanceof SQL ) { /** @var SQL $db */ $ schema = new Schema ( $ db ) ; $ tables = $ schema -> getTables ( ) ; foreach ( $ deletable as $ item ) if ( in_array ( $ item , $ tables ) ) $ schema -> dropTable ( $ item ) ; } elseif ( $ db instanceof Mongo ) { /** @var Mongo $db */ foreach ( $ deletable as $ item ) $ db -> selectCollection ( $ item ) -> drop ( ) ; } }
erase all model data, handle with care
@param null $db
@param null $table
erase all model data handle with care