Example #1
0
    function compile($options)
    {
        if (!file_exists($this->_fileName)) {
            throw org_glizy_compilers_CompilerException::fileNotFound($this->_fileName);
        }
        $this->initOutput();
        $xml = org_glizy_ObjectFactory::createObject('org.glizy.parser.XML');
        $xml->loadAndParseNS($this->_fileName);
        $xmlRootNode = $xml->documentElement;
        $className = glz_basename($this->_cacheObj->getFileName());
        $tableName = $xmlRootNode->getAttribute('model:tableName');
        if (empty($tableName)) {
            throw org_glizy_compilers_ModelException::missingTableName($this->_fileName);
        }
        $joinFields = $xmlRootNode->hasAttribute('model:joinFields') ? explode(',', $xmlRootNode->getAttribute('model:joinFields')) : null;
        $modelType = $xmlRootNode->hasAttribute('model:type') ? $xmlRootNode->getAttribute('model:type') : 'activeRecord';
        $dbConnection = $xmlRootNode->hasAttribute('model:connection') ? $dbConnection = $xmlRootNode->getAttribute('model:connection') : '0';
        $usePrefix = $xmlRootNode->hasAttribute('model:usePrefix') && $xmlRootNode->getAttribute('model:usePrefix') == 'true' ? 'true' : 'false';
        $languageField = $xmlRootNode->hasAttribute('model:languageField') ? '$this->setLanguageField(\'' . $xmlRootNode->getAttribute('model:languageField') . '\');' : '';
        $siteField = $xmlRootNode->hasAttribute('model:siteField') ? '$this->setSiteField(\'' . $xmlRootNode->getAttribute('model:siteField') . '\');' : '';
        $originalClassName = isset($options['originalClassName']) ? '$this->_className = \'' . $options['originalClassName'] . '\';' : '';
        if ($modelType == '2tables') {
            list($tableName, $detailTableName) = explode(',', $tableName);
        }
        $baseclassName = $this->resolveBaseClass($modelType);
        $fields = $this->compileFields($xmlRootNode);
        $queries = $this->compileQueries($xmlRootNode);
        $script = $this->compileScript($xmlRootNode);
        if ($baseclassName['type'] == 'activeRecord') {
            $compiledOutput = <<<EOD
class {$className} extends {$baseclassName['activeRecord']}
{
    function __construct(\$connectionNumber={$dbConnection}) {
        parent::__construct(\$connectionNumber);
        {$originalClassName}
        \$this->setTableName('{$tableName}',
                {$usePrefix} ? org_glizy_dataAccessDoctrine_DataAccess::getTablePrefix(\$connectionNumber) : '' );

        \$sm = new org_glizy_dataAccessDoctrine_SchemaManager(\$this->connection);
        \$fields = \$sm->getFields(\$this->getTableName());

        foreach (\$fields as \$field) {
            \$this->addField(\$field);
        }

        {$fields}

        if (__Config::get('MULTISITE_ENABLED')) {
            {$siteField}
        }
    }

    public function createRecordIterator() {
        return new {$className}_iterator(\$this);
    }

    {$script['model']}
    {$queries}
}
class {$className}_iterator extends {$baseclassName['recordIterator']}
{
    {$script['iterator']}
}
EOD;
        } elseif ($baseclassName['type'] == '2tables') {
            $compiledOutput = <<<EOD
class {$className} extends {$baseclassName['activeRecord']}
{
    function __construct(\$connectionNumber={$dbConnection}) {
        parent::__construct(\$connectionNumber);
        \$this->setTableName('{$tableName}',
                {$usePrefix} ? org_glizy_dataAccessDoctrine_DataAccess::getTablePrefix(\$connectionNumber) : '' );

        \$this->setDetailTableName('{$detailTableName}',
                {$usePrefix} ? org_glizy_dataAccessDoctrine_DataAccess::getTablePrefix(\$connectionNumber) : '' );

        \$this->setJoinFields('{$joinFields['0']}', '{$joinFields['1']}');

        \$sm = new org_glizy_dataAccessDoctrine_SchemaManager(\$this->connection);
        \$fields = \$sm->getFields(\$this->getTableName());

        foreach (\$fields as \$field) {
            \$this->addField(\$field);
        }

        \$fields = \$sm->getFields(\$this->getDetailTableName());

        foreach (\$fields as \$field) {
            \$this->addField(\$field, true);
        }

        {$fields}

        {$languageField}

        if (__Config::get('MULTISITE_ENABLED')) {
            {$siteField}
        }
    }

    public function createRecordIterator() {
        return new {$className}_iterator(\$this);
    }

    {$script['model']}
    {$queries}
}
class {$className}_iterator extends {$baseclassName['recordIterator']}
{
    {$script['iterator']}
}
EOD;
        } else {
            $compiledOutput = <<<EOD
class {$className} extends {$baseclassName['activeRecord']}
{
    function __construct(\$connectionNumber={$dbConnection}) {
        parent::__construct(\$connectionNumber);
        {$originalClassName}
        \$this->setTableName('{$tableName}',
                {$usePrefix} ? org_glizy_dataAccessDoctrine_DataAccess::getTablePrefix(\$connectionNumber) : '' );
        \$this->setType('{$tableName}');

        {$fields}
    }

    public function createRecordIterator() {
        return new {$className}_iterator(\$this);
    }

    {$script['model']}
    {$queries}
}
class {$className}_iterator extends {$baseclassName['recordIterator']}
{
    {$script['iterator']}
}
EOD;
        }
        $this->output .= $compiledOutput;
        return $this->save();
    }
Example #2
0
 /**
  * @param         $classPath
  * @param integer $connectionNumber
  *
  * @return org_glizy_dataAccess_ActiveRecord
  * @throws org_glizy_compilers_CompilerException
  */
 static function &createModel($classPath, $connectionNumber = null)
 {
     $classInfo = org_glizy_ObjectFactory::resolveClassNew($classPath);
     if (isset($classInfo['path'])) {
         $compiler = org_glizy_ObjectFactory::createObject('org.glizy.compilers.Model');
         $compiledFileName = $compiler->verify($classInfo['path'], array('originalClassName' => $classInfo['originalClassName']));
         require_once $compiledFileName;
         $className = glz_basename($compiledFileName);
         $newObj = $connectionNumber ? new $className($connectionNumber) : new $className();
         $classMap =& org_glizy_ObjectValues::get('org.glizy.ObjectFactory', 'ClassMap', array());
         $classMap[$classPath] = $className;
     } else {
         if (isset($classInfo['class'])) {
             $newObj = $connectionNumber ? new $classInfo['class']($connectionNumber) : new $classInfo['class']();
         } else {
             throw org_glizy_compilers_CompilerException::fileNotFound($classPath);
         }
     }
     return $newObj;
 }