Exemplo n.º 1
0
	/**
	 * Internal function to parse and handle the DBSchema in the component.xml file.
	 * This is used for installations and upgrades.
	 *
	 * @param boolean $install   Set to false to force uninstall/disable mode.
	 * @param int     $verbosity (default 0) 0: standard output, 1: real-time, 2: real-time verbose output.
	 *
	 * @throws DMI_Query_Exception
	 * @throws Exception
	 * @return boolean | int
	 */
	public function _parseDBSchema($install = true, $verbosity = 0) {
		// I need to get the schema definitions first.
		$node   = $this->_xmlloader->getElement('dbschema');
		$prefix = $node->getAttribute('prefix');
		/** @var \Core\Datamodel\BackendInterface $db */
		$db     = \Core\db();

		$changes = array();

		Core\Utilities\Logger\write_debug('Installing database schema for ' . $this->getName());

		// Get the table structure as it exists in the database first, this will be the comparison point.
		$classes = $this->getModelList();

		// Do the actual processing of every Model.
		foreach ($classes as $m => $file) {
			if(!class_exists($m)) require_once($file);

			$schema = ModelFactory::GetSchema($m);
			$tablename = $m::GetTableName();

			if($verbosity == 2){
				CLI::PrintActionStart('Processing database table ' . $tablename);
			}

			try{
				if ($db->tableExists($tablename)) {
					// modifyTable will not change the table if there are no changes to perform and
					// will return a list of the changes for reporting reasons.
					$res = $db->modifyTable($tablename, $schema);
					if($res !== false){
						// Changes detected!
						$changes[] = 'Modified table ' . $tablename;
						// $changes[] = '[' . $d['type'] . '] ' . $d['title'];
						$changes = array_merge($changes, $res);
						if($verbosity == 2){
							CLI::PrintActionStatus('ok');
						}
					}
					else{
						// No changes detected.
						if($verbosity == 2){
							CLI::PrintActionStatus('skip');
						}
					}
				}
				else {
					// Pass this schema into the DMI processor for create table.
					\Core\db()->createTable($tablename, $schema);
					$changes[] = 'Created table ' . $tablename;
					if($verbosity == 2){
						CLI::PrintActionStatus('ok');
					}
				}
			}
			catch(DMI_Query_Exception $e){
				error_log($e->query . "\n<br/>(original table " . $tablename . ")");
				// Append the table name since otherwise it may be "_tmptable"... which does not provide any useful information!
				$e->query = $e->query . "\n<br/>(original table " . $tablename . ")";
				//echo '<pre>' . $e->getTraceAsString() . '</pre>'; // DEBUG //
				throw $e;
			}
		}

		return sizeof($changes) ? $changes : false;
	} // public function _parseDBSchema()