/**
  * Add code in ObjectBuilder::preSave
  *
  * @return string The code to put at the hook
  */
 public function preSave(PHP5ObjectBuilder $builder)
 {
     $const = $builder->getColumnConstant($this->getColumnForParameter('slug_column'));
     $pattern = $this->getParameter('slug_pattern');
     $script = "\nif (\$this->isColumnModified({$const}) && \$this->{$this->getColumnGetter()}()) {\n    \$this->{$this->getColumnSetter()}(\$this->makeSlugUnique(\$this->{$this->getColumnGetter()}()));";
     if ($pattern && false === $this->booleanValue($this->getParameter('permanent'))) {
         $script .= "\n} elseif (";
         $count = preg_match_all('/{([a-zA-Z]+)}/', $pattern, $matches, PREG_PATTERN_ORDER);
         foreach ($matches[1] as $key => $match) {
             $columnName = $this->underscore(ucfirst($match));
             $column = $this->getTable()->getColumn($columnName);
             if (null == $column && $this->getTable()->hasBehavior('symfony_i18n')) {
                 $i18n = $this->getTable()->getBehavior('symfony_i18n');
                 $column = $i18n->getI18nTable()->getColumn($columnName);
             }
             if (null == $column) {
                 throw new \InvalidArgumentException(sprintf('The pattern %s is invalid  the column %s is not found', $pattern, $match));
             }
             $columnConst = $builder->getColumnConstant($column);
             $script .= "\$this->isColumnModified({$columnConst})" . ($key < $count - 1 ? " || " : "");
         }
         $script .= ") {\n    \$this->{$this->getColumnSetter()}(\$this->createSlug());";
     }
     if (null == $pattern && false === $this->booleanValue($this->getParameter('permanent'))) {
         $script .= "\n} else {\n    \$this->{$this->getColumnSetter()}(\$this->createSlug());\n}";
     } else {
         $script .= "\n} elseif (!\$this->{$this->getColumnGetter()}()) {\n    \$this->{$this->getColumnSetter()}(\$this->createSlug());\n}";
     }
     return $script;
 }
    protected function addGetLastVersions(&$script)
    {
        $versionTable = $this->behavior->getVersionTable();
        $versionARClassname = $this->builder->getNewStubObjectBuilder($versionTable)->getClassname();
        $versionForeignColumn = $versionTable->getColumn($this->behavior->getParameter('version_column'));
        $fks = $versionTable->getForeignKeysReferencingTable($this->table->getName());
        $relCol = $this->builder->getRefFKPhpNameAffix($fks[0], $plural = true);
        $versionGetter = 'get' . $relCol;
        $versionPeerBuilder = $this->builder->getNewStubPeerBuilder($versionTable);
        $this->builder->declareClassFromBuilder($versionPeerBuilder);
        $versionPeer = $versionPeerBuilder->getClassname();
        $script .= <<<EOF
/**
 * retrieve the last \$number versions.
 *
 * @param integer \$number the number of record to return.
 * @param {$this->getVersionQueryClassName()}|Criteria \$criteria Additional criteria to filter.
 * @param PropelPDO \$con An optional connection to use.
 *
 * @return PropelCollection|{$versionARClassname}[] List of {$versionARClassname} objects
 */
public function getLastVersions(\$number = 10, \$criteria = null, PropelPDO \$con = null)
{
    \$criteria = {$this->getVersionQueryClassName()}::create(null, \$criteria);
    \$criteria->addDescendingOrderByColumn({$versionPeer}::VERSION);
    \$criteria->limit(\$number);

    return \$this->{$versionGetter}(\$criteria, \$con);
}
EOF;
    }
 /**
  * Adds the function body for the save method
  * @param      string &$script The script will be modified in this method.
  * @see        addSave()
  **/
 protected function addSaveBody(&$script)
 {
     $add = false;
     $columns = $this->getTable()->getColumns();
     // created_at, updated_at columns
     $added_now = false;
     $set_now = "\n\t\t\$now = new DateTime('now');";
     foreach ($columns as $column) {
         if ($column->getPhpName() == 'CreatedAt' && $column->getType() == 'TIMESTAMP') {
             $add = true;
             if (!$added_now) {
                 $script .= $set_now;
                 $added_now = true;
             }
             $script .= "\n\t\tif(!\$this->getCreatedAt()) \$this->setCreatedAt(clone \$now);";
         } elseif ($column->getPhpName() == 'UpdatedAt' && $column->getType() == 'TIMESTAMP') {
             $add = true;
             if (!$added_now) {
                 $script .= $set_now;
                 $added_now = true;
             }
             $script .= "\n\t\t\$this->setUpdatedAt(clone \$now);";
         }
     }
     // pass it on
     if ($add) {
         $script .= "\n";
     }
     parent::addSaveBody($script);
 }
 /**
  * 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;
 }
 public function objectMethods(PHP5ObjectBuilder $builder)
 {
     $script = "\n/**\n * In addition to checking if this object has been modified, it can also check if any of the related objects have been modified.\n *\n * @param bool \$check_relations\n * @return bool\n */\npublic function isModified(\$check_relations = false)\n{\n    if (!\$check_relations) {\n        return parent::isModified();\n    }\n\n    if (\$this->alreadyInModified) {\n        return null;\n    }\n\n    \$this->alreadyInModified = true;\n\n    \$modified = false;\n\n    if (parent::isModified()) {\n        \$modified = true;\n    } else {";
     $table = $builder->getTable();
     if (count($table->getForeignKeys())) {
         foreach ($table->getForeignKeys() as $fk) {
             $aVarName = $builder->getFKVarName($fk);
             $script .= "\n        if (\$this->{$aVarName} !== null) {\n            if (\$this->" . $aVarName . "->isModified() || \$this->" . $aVarName . "->isNew()) {\n                \$modified = true;\n            }\n        }";
         }
     }
     foreach ($table->getReferrers() as $refFK) {
         if ($refFK->isLocalPrimaryKey()) {
             $varName = $builder->getPKRefFKVarName($refFK);
             $script .= "\n        if (\$this->{$varName} !== null) {\n            if (\$this->{$varName}->isDeleted() || \$this->{$varName}->isNew() || \$this->{$varName}->isModified()) {\n                \$modified = true;\n            }\n        }\n                ";
         } else {
             //                $this->addRefFkScheduledForDeletion($script, $refFK);
             $collName = $builder->getRefFKCollVarName($refFK);
             $script .= "\n        if (\$this->{$collName} !== null) {\n            foreach (\$this->{$collName} as \$referrerFK) {\n                if (\$referrerFK->isDeleted() || \$referrerFK->isNew() || \$referrerFK->isModified()) {\n                    \$modified = true;\n                    break;\n                }\n            }\n        }";
         }
     }
     $script .= "\n    }\n\n    \$this->alreadyInModified = false;\n\n    return \$modified;\n}";
     return $script;
 }
 public function objectMethods(PHP5ObjectBuilder $builder)
 {
     if ($this->withUpdatedAt()) {
         return "\n/**\n * Mark the current object so that the update date doesn't get updated during next save\n *\n * @return     " . $builder->getStubObjectBuilder()->getClassname() . " The current object (for fluent API support)\n */\npublic function keepUpdateDateUnchanged()\n{\n    \$this->modifiedColumns[] = " . $this->getColumnConstant('update_column', $builder) . ";\n\n    return \$this;\n}\n";
     }
 }
Example #7
0
 protected function addBuildPkeyCriteriaClose(&$script)
 {
     $table = $this->getTable();
     if (!$table->getColumn(self::KALTURA_COLUMN_UPDATED_AT)) {
         return parent::addBuildPkeyCriteriaClose($script);
     }
     $script .= "\n\t\t\n\t\tif(\$this->alreadyInSave)\n\t\t{";
     if ($table->containsColumn(self::KALTURA_COLUMN_CUSTOM_DATA)) {
         $script .= "\n\t\t\tif (\$this->isColumnModified(" . $this->getPeerClassname() . "::CUSTOM_DATA))\n\t\t\t{\n\t\t\t\tif (!is_null(\$this->custom_data_md5))\n\t\t\t\t\t\$criteria->add(" . $this->getPeerClassname() . "::CUSTOM_DATA, \"MD5(cast(\" . " . $this->getPeerClassname() . "::CUSTOM_DATA . \" as char character set latin1)) = '\$this->custom_data_md5'\", Criteria::CUSTOM);\n\t\t\t\t\t//casting to latin char set to avoid mysql and php md5 difference\n\t\t\t\telse \n\t\t\t\t\t\$criteria->add(" . $this->getPeerClassname() . "::CUSTOM_DATA, NULL, Criteria::ISNULL);\n\t\t\t}\n\t\t\t";
     }
     $script .= "\n\t\t\tif (count(\$this->modifiedColumns) == 2 && \$this->isColumnModified(" . $this->getPeerClassname() . "::UPDATED_AT))\n\t\t\t{\n\t\t\t\t\$theModifiedColumn = null;\n\t\t\t\tforeach(\$this->modifiedColumns as \$modifiedColumn)\n\t\t\t\t\tif(\$modifiedColumn != " . $this->getPeerClassname() . "::UPDATED_AT)\n\t\t\t\t\t\t\$theModifiedColumn = \$modifiedColumn;\n\t\t\t\t\t\t\n\t\t\t\t\$atomicColumns = " . $this->getPeerClassname() . "::getAtomicColumns();\n\t\t\t\tif(in_array(\$theModifiedColumn, \$atomicColumns))\n\t\t\t\t\t\$criteria->add(\$theModifiedColumn, \$this->getByName(\$theModifiedColumn, BasePeer::TYPE_COLNAME), Criteria::NOT_EQUAL);\n\t\t\t}\n\t\t}\t\t\n\n\t\treturn \$criteria;\n\t}\n";
 }
 public function getDefaultValueString(Column $col)
 {
     return parent::getDefaultValueString($col);
 }
Example #9
0
    protected function addClassClose(&$script)
    {
        parent::addClassClose($script);
        $behaviors = $this->getTable()->getAttribute('behaviors');
        if ($behaviors) {
            $behavior_file_name = 'Base' . $this->getTable()->getPhpName() . 'Behaviors';
            $behavior_file_path = ClassTools::getFilePath($this->getStubObjectBuilder()->getPackage() . '.om', $behavior_file_name);
            $behavior_include_script = <<<EOF


if (sfProjectConfiguration::getActive() instanceof sfApplicationConfiguration)
{
  include_once '%s';
}

EOF;
            $script .= sprintf($behavior_include_script, $behavior_file_path);
        }
    }
 protected function addClassClose(&$script)
 {
     parent::addClassClose($script);
     $behaviors = $this->getTable()->getAttribute('behaviors');
     if ($behaviors) {
         $behavior_file_name = 'Base' . $this->getTable()->getPhpName() . 'Behaviors';
         $behavior_file_path = ClassTools::getFilePath($this->getStubObjectBuilder()->getPackage() . '.om', $behavior_file_name);
         $script .= sprintf("\ninclude_once '%s';\n", $behavior_file_path);
     }
 }
 public function __construct(Table $table)
 {
     parent::__construct($table);
     $this->initialize($table);
 }
Example #12
0
 /**
  * Adds the mutator open body part
  * @param      string &$script The script will be modified in this method.
  * @param      Column $col The current column.
  * @see        addMutatorOpen()
  **/
 protected function addMutatorOpenBody(&$script, Column $col)
 {
     parent::addMutatorOpenBody($script, $col);
     $clo = strtolower($col->getName());
     if (in_array($clo, self::$systemColumns)) {
         return;
     }
     $fullColumnName = $this->getColumnConstant($col);
     $script .= "\n\t\tif(!isset(\$this->oldColumnsValues[{$fullColumnName}]))\n\t\t\t\$this->oldColumnsValues[{$fullColumnName}] = \$this->{$clo};\n";
 }
 protected function addBuildPkeyCriteriaClose(&$script)
 {
     $table = $this->getTable();
     if (!$table->getColumn(self::KALTURA_COLUMN_UPDATED_AT)) {
         return parent::addBuildPkeyCriteriaClose($script);
     }
     $script .= "\n\t\t\n\t\tif(\$this->alreadyInSave && count(\$this->modifiedColumns) == 2 && \$this->isColumnModified(" . $this->getPeerClassname() . "::UPDATED_AT))\n\t\t{\n\t\t\t\$theModifiedColumn = null;\n\t\t\tforeach(\$this->modifiedColumns as \$modifiedColumn)\n\t\t\t\tif(\$modifiedColumn != " . $this->getPeerClassname() . "::UPDATED_AT)\n\t\t\t\t\t\$theModifiedColumn = \$modifiedColumn;\n\t\t\t\t\t\n\t\t\t\$atomicColumns = " . $this->getPeerClassname() . "::getAtomicColumns();\n\t\t\tif(in_array(\$theModifiedColumn, \$atomicColumns))\n\t\t\t\t\$criteria->add(\$theModifiedColumn, \$this->getByName(\$theModifiedColumn, BasePeer::TYPE_COLNAME), Criteria::NOT_EQUAL);\n\t\t}\n\n\t\treturn \$criteria;\n\t}\n";
 }