Location: PHPKode > scripts > QXML > qxml/Propel2Doctrine.class.php
<?php

include_once("Spyc.class.php");
include_once("QTransform.class.php");

class Propel2Doctrine extends QTransform {

	/**
	 * Camelize
	 *
	 * @param string $lower_case_and_underscored_word
	 * @return string
	 */
	public static function camelize($lower_case_and_underscored_word)
	{
		$replace = str_replace(" ", "", ucwords(str_replace("_", " ", $lower_case_and_underscored_word)));
		return $replace;
	}
	
	/**
	 * transform
	 *
	 * @param string $dbdesignerXML
	 * @param string $dbdesignerToPropelXSL
	 * @param string $doctrineSchema
	 * @return string
	 */
	public function transform($dbdesignerXML, $dbdesignerToPropelXSL = "dbd2propel.xsl", $doctrineSchema = "schema.yml"){

		$xmlDBDesigner = simplexml_load_file($dbdesignerXML);
		$propelSchema = $this->toXML($xmlDBDesigner, $dbdesignerToPropelXSL);

		$xml = new DOMDocument();
		$xml->loadXML($propelSchema);

		$xpath = new DOMXPath($xml);

		$doctrineSchema = array ( );

		$tables = $xpath->query ( "//table" );

		for($i = 0; $i < $tables->length; $i ++) {
			$table = $tables->item ( $i );
			$tableName = $table->getAttribute ( 'name' );
			$class = self::camelize ( $tableName );

			$doctrineSchema [$class] ['tableName'] = $tableName;

			$tableChildNodes = $table->childNodes;
			for($j = 0; $j < $tableChildNodes->length; $j ++) {
				$tableChild = $tableChildNodes->item ( $j );
				$nodeName = $tableChild->nodeName;
				if ($nodeName == "column") {
					$columnName = $tableChild->getAttribute ( 'name' );

					switch ( $tableChild->getAttribute ( 'type' )) {
						case 'INTEGER' :
							$columnType = 'integer';
							$columnSize = '4';
							break;

						case 'VARCHAR' :
							$columnType = 'string';
							$columnSize = $tableChild->getAttribute ( 'size' );
							break;

						case 'CHAR' :
							$columnType = 'string';
							$columnSize = 1;
							break;

						case 'LONGVARCHAR' :
							$columnType = 'string';
							$columnSize = 4000;
							break;

						case 'TIMESTAMP' :
							$columnType = 'timestamp';
							$columnSize = '';
							break;

						case 'DATE' :
							$columnType = 'date';
							$columnSize = '';
							break;

						case 'FLOAT' :
							$columnType = 'float';
							$columnSize = '';
							break;

						case 'BOOLEAN' :
							$columnType = 'boolean';
							$columnSize = '';
							break;

						default :
							$columnType = '';
							$columnSize = '';
							break;
					}

					$doctrineSchema [$class] ['columns'] [$columnName] = array ('type' => $columnType, 'size' => $columnSize );

					if ($tableChild->hasAttribute ( 'default' )) {
						$doctrineSchema [$class] ['columns'] [$columnName] ['default'] = $tableChild->getAttribute ( 'default' );
					}

					if ($tableChild->getAttribute ( 'required' ) == "true") {
						$doctrineSchema [$class] ['columns'] [$columnName] ['notnull'] = "true";
					}

					if ($tableChild->getAttribute ( 'primaryKey' ) == "true") {
						$doctrineSchema [$class] ['columns'] [$columnName] ['primary'] = "true";
					}

					if ($tableChild->getAttribute ( 'autoIncrement' ) == "true") {
						$doctrineSchema [$class] ['columns'] [$columnName] ['autoincrement'] = "true";
					}
				} elseif ($nodeName == "foreign-key") {
					$foreignTable = $tableChild->getAttribute ( 'foreignTable' );
					$foreignClass = self::camelize ( $foreignTable );

					$foreignKeyChilds = $tableChild->childNodes;
					$referenceNode = null;

					for($k = 0; $k < $foreignKeyChilds->length; $k ++) {
						if ($foreignKeyChilds->item ( $k )->nodeType == XML_ELEMENT_NODE && $foreignKeyChilds->item ( $k )->nodeName == "reference") {
							$referenceNode = $foreignKeyChilds->item ( $k );
							break;
						}
					}
					if (! $referenceNode)
					continue;

					$onDelete = ($tableChild->getAttribute ( 'onDelete' ) == 'setnull') ? 'null' : $tableChild->getAttribute ( 'onDelete' );
					$alias = self::camelize ( substr ( $referenceNode->getAttribute ( 'local' ), 0, - 3 ) );
					// local = "id", so $alias = "". We need set $alias to $foreignClass
					if ($alias == "") {
						$alias = $foreignClass;
					}
					$doctrineSchema [$class] ['relations'] [$alias] = array ('class' => $foreignClass, 'foreign' => $referenceNode->getAttribute ( 'foreign' ), 'foreignAlias' => $class . 's', 'alias' => $alias, 'local' => $referenceNode->getAttribute ( 'local' ), 'onDelete' => $onDelete );
				} elseif ($nodeName == "unique") {
					$indexName = $tableChild->getAttribute ( 'name' );

					$foreignKeyChilds = $tableChild->childNodes;
					$fields = array ( );
					for($k = 0; $k < $foreignKeyChilds->length; $k ++) {
						if ($foreignKeyChilds->item ( $k )->nodeType == XML_ELEMENT_NODE && $foreignKeyChilds->item ( $k )->nodeName == "unique-column") {
							$fields [] = $foreignKeyChilds->item ( $k )->getAttribute ( 'name' );
						}
					}

					$doctrineSchema [$class] ['indexes'] [$indexName] = array ('fields' => $fields, 'type' => 'unique' );
				}
			}
		}

		$spyc = new Spyc();
		return $spyc->dump ( $doctrineSchema ) ;

	}
}

?>
Return current item: QXML