protected function addPopulateFromVersion(&$script)
 {
     $ARclassName = $this->getActiveRecordClassName();
     $versionTable = $this->behavior->getVersionTable();
     $versionColumnName = $versionTable->getColumn($this->behavior->getParameter('version_column'))->getPhpName();
     $versionARClassname = $this->builder->getNewStubObjectBuilder($versionTable)->getClassname();
     $tablePKs = $this->table->getPrimaryKey();
     $primaryKeyName = $tablePKs[0]->getPhpName();
     $script .= "\n/**\n * Sets the properties of the curent object to the value they had at a specific version\n *\n * @param   {$versionARClassname} \$version The version object to use\n * @param   PropelPDO \$con the connection to use\n * @param   array \$loadedObjects objects thats been loaded in a chain of populateFromVersion calls on referrer or fk objects.\n *\n * @return  {$ARclassName} The current object (for fluent API support)\n */\npublic function populateFromVersion(\$version, \$con = null, &\$loadedObjects = array())\n{\n";
     $script .= "\n    \$loadedObjects['{$ARclassName}'][\$version->get{$primaryKeyName}()][\$version->get{$versionColumnName}()] = \$this;";
     foreach ($this->table->getColumns() as $col) {
         $script .= "\n    \$this->set" . $col->getPhpName() . "(\$version->get" . $col->getPhpName() . "());";
     }
     foreach ($this->behavior->getVersionableFks() as $fk) {
         $foreignTable = $fk->getForeignTable();
         $foreignVersionTable = $fk->getForeignTable()->getBehavior($this->behavior->getName())->getVersionTable();
         $relatedClassname = $this->builder->getNewStubObjectBuilder($foreignTable)->getClassname();
         $relatedVersionQueryBuilder = $this->builder->getNewStubQueryBuilder($foreignVersionTable);
         $this->builder->declareClassFromBuilder($relatedVersionQueryBuilder);
         $relatedVersionQueryClassname = $relatedVersionQueryBuilder->getClassname();
         $fkColumnName = $fk->getLocalColumnName();
         $fkColumnPhpName = $fk->getLocalColumn()->getPhpName();
         $fkVersionColumnPhpName = $versionTable->getColumn($fkColumnName . '_version')->getPhpName();
         $fkPhpname = $this->builder->getFKPhpNameAffix($fk, $plural = false);
         // FIXME: breaks lazy-loading
         $script .= "\n    if (\$fkValue = \$version->get{$fkColumnPhpName}()) {\n        if (isset(\$loadedObjects['{$relatedClassname}']) && isset(\$loadedObjects['{$relatedClassname}'][\$fkValue]) && isset(\$loadedObjects['{$relatedClassname}'][\$fkValue][\$version->get{$fkVersionColumnPhpName}()])) {\n            \$related = \$loadedObjects['{$relatedClassname}'][\$fkValue][\$version->get{$fkVersionColumnPhpName}()];\n        } else {\n            \$related = new {$relatedClassname}();\n            \$relatedVersion = {$relatedVersionQueryClassname}::create()\n                ->filterBy{$fk->getForeignColumn()->getPhpName()}(\$fkValue)\n                ->filterByVersion(\$version->get{$fkVersionColumnPhpName}())\n                ->findOne(\$con);\n            \$related->populateFromVersion(\$relatedVersion, \$con, \$loadedObjects);\n            \$related->setNew(false);\n        }\n        \$this->set{$fkPhpname}(\$related);\n    }";
     }
     foreach ($this->behavior->getVersionableReferrers() as $fk) {
         $foreignTable = $fk->getTable();
         $foreignBehavior = $foreignTable->getBehavior($this->behavior->getName());
         $foreignVersionTable = $foreignBehavior->getVersionTable();
         $fkColumn = $foreignVersionTable->getFirstPrimaryKeyColumn();
         $fkVersionColumn = $foreignVersionTable->getColumn($this->behavior->getParameter('version_column'));
         $relatedClassname = $this->builder->getNewStubObjectBuilder($foreignTable)->getClassname();
         $relatedVersionQueryBuilder = $this->builder->getNewStubQueryBuilder($foreignVersionTable);
         $this->builder->declareClassFromBuilder($relatedVersionQueryBuilder);
         $relatedVersionQueryClassname = $relatedVersionQueryBuilder->getClassname();
         $relatedVersionPeerBuilder = $this->builder->getNewStubPeerBuilder($foreignVersionTable);
         $relatedVersionPeerClassname = $relatedVersionPeerBuilder->getClassname();
         if ($fk->isLocalPrimaryKey()) {
             $fkPhpName = $this->builder->getRefFKPhpNameAffix($fk, $plural = false);
             $fkColumnId = $this->behavior->getReferrerIdsColumn($fk);
             $fkColumnVersion = $this->behavior->getReferrerVersionsColumn($fk);
             $fkColumnVersionPhpName = $fkColumnVersion->getPhpName();
             $this->builder->declareClassFromBuilder($relatedVersionPeerBuilder);
             $script .= "\n    if (\$fkValue = \$version->get{$fkColumnId->getPhpName()}()) {\n        if (isset(\$loadedObjects['{$relatedClassname}']) && isset(\$loadedObjects['{$relatedClassname}'][\$fkValue]) && isset(\$loadedObjects['{$relatedClassname}'][\$fkValue][\$version->get{$fkColumnVersionPhpName}()])) {\n            \$related = \$loadedObjects['{$relatedClassname}'][\$fkValue][\$version->get{$fkColumnVersionPhpName}()];\n        } else {\n            \$related = new {$relatedClassname}();\n            \$relatedVersion = {$relatedVersionQueryClassname}::create()\n                ->filterBy{$fk->getLocalColumn()->getPhpName()}(\$fkValue)\n                ->filterByVersion(\$version->get{$fkColumnVersionPhpName}())\n                ->findOne(\$con);\n            \$related->populateFromVersion(\$relatedVersion, \$con, \$loadedObjects);\n            \$related->setNew(false);\n        }\n        \$this->set{$fkPhpName}(\$related);\n    }";
         } else {
             $fkPhpNames = $this->builder->getRefFKPhpNameAffix($fk, $plural = true);
             $fkPhpName = $this->builder->getRefFKPhpNameAffix($fk, $plural = false);
             $fkColumnIds = $this->behavior->getReferrerIdsColumn($fk);
             $fkColumnVersions = $this->behavior->getReferrerVersionsColumn($fk);
             $this->builder->declareClassFromBuilder($relatedVersionPeerBuilder);
             $script .= "\n    if (\$fkValues = \$version->get{$fkColumnIds->getPhpName()}()) {\n        \$this->clear{$fkPhpNames}();\n        \$fkVersions = \$version->get{$fkColumnVersions->getPhpName()}();\n        \$query = {$relatedVersionQueryClassname}::create();\n        foreach (\$fkValues as \$key => \$value) {\n            \$c1 = \$query->getNewCriterion({$this->builder->getColumnConstant($fkColumn, $relatedVersionPeerClassname)}, \$value);\n            \$c2 = \$query->getNewCriterion({$this->builder->getColumnConstant($fkVersionColumn, $relatedVersionPeerClassname)}, \$fkVersions[\$key]);\n            \$c1->addAnd(\$c2);\n            \$query->addOr(\$c1);\n        }\n        foreach (\$query->find(\$con) as \$relatedVersion) {\n            if (isset(\$loadedObjects['{$relatedClassname}']) && isset(\$loadedObjects['{$relatedClassname}'][\$relatedVersion->get{$fkColumn->getPhpName()}()]) && isset(\$loadedObjects['{$relatedClassname}'][\$relatedVersion->get{$fkColumn->getPhpName()}()][\$relatedVersion->get{$fkVersionColumn->getPhpName()}()])) {\n                \$related = \$loadedObjects['{$relatedClassname}'][\$relatedVersion->get{$fkColumn->getPhpName()}()][\$relatedVersion->get{$fkVersionColumn->getPhpName()}()];\n            } else {\n                \$related = new {$relatedClassname}();\n                \$related->populateFromVersion(\$relatedVersion, \$con, \$loadedObjects);\n                \$related->setNew(false);\n            }\n            \$this->add{$fkPhpName}(\$related);\n        }\n\n        \$this->resetPartial{$fkPhpNames}(false);\n    }";
         }
     }
     $script .= "\n\n    return \$this;\n}\n";
 }
 /**
  * Add object attributes to the built class.
  *
  * @param PHP5ObjectBuilder $builder
  *
  * @return string The PHP code to be added to the builder.
  */
 public function objectAttributes(PHP5ObjectBuilder $builder)
 {
     if (!$this->behavior->hasArchiveClass()) {
         $builder->declareClassFromBuilder($builder->getNewStubQueryBuilder($this->behavior->getArchiveTable()));
     }
     $script = '';
     if ($this->behavior->isArchiveOnInsert()) {
         $script .= "protected \$archiveOnInsert = true;\n";
     }
     if ($this->behavior->isArchiveOnUpdate()) {
         $script .= "protected \$archiveOnUpdate = true;\n";
     }
     if ($this->behavior->isArchiveOnDelete()) {
         $script .= "protected \$archiveOnDelete = true;\n";
     }
     return $script;
 }