Skip to content

Commit

Permalink
Fixed race condition in PRE_SAVE of the sql persister and fixed relat…
Browse files Browse the repository at this point in the history
…ion methods

Use setExpression instead of PhpConstant for default values in parameters.
SaveEvent does not have separation of entity state anymore (instead of update/insert, we have both in an array)
  • Loading branch information
marcj committed Nov 30, 2016
1 parent 35d5a63 commit e8e0db9
Show file tree
Hide file tree
Showing 39 changed files with 252 additions and 359 deletions.
4 changes: 4 additions & 0 deletions .idea/blade.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Propel/Common/Types/SQL/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function build(AbstractBuilder $builder, Field $field)
$property = $builder->getDefinition()->getProperty($field->getName());

if (!$property->hasValue()) {
$property->setValue(PhpConstant::create('[]'));
$property->setExpression('[]');
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public function postSave(RepositoryBuilder $builder)
$aggregateName = ucfirst($this->getParameter('aggregate_name'));

return "
\$this->updateRelated{$relationName}{$aggregateName}(\$event->getEntitiesToInsert());
\$this->updateRelated{$relationName}{$aggregateName}(\$event->getEntitiesToUpdate());
\$this->updateRelated{$relationName}{$aggregateName}(\$event->getEntities());
";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ public function process()
{
/** @var AggregateFieldRelationBehavior $behavior */
$behavior = $this->getBehavior();


$relation = $behavior->getRelation();
// $relationName = $behavior->getRelationName();
$refRelationName = $this->getRefRelationPhpName($relation);

$relationName = $behavior->getRelationName();
// $variableName = $relationName . ucfirst($behavior->getParameter('aggregate_name'));
$updateMethodName = $behavior->getParameter('update_method');
$aggregateName = ucfirst($behavior->getParameter('aggregate_name'));

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -97,39 +97,22 @@ $createSlug = function($entity) use ($makeSlugUnique, $cleanupSlugPart, $limitSl
return $slug;
};

$slugs = [];
foreach ($event->getEntitiesToInsert() as $entity) {
$slug = $entity->getSlug();
if (null === $slug || '' === $slug) {
$slug = $createSlug($entity);
} else {
$slug = $cleanupSlugPart($slug);
$slug = $limitSlugSize($slug);
$slug = $makeSlugUnique($slug{{#scopeField}}, '{{separator}}', false, $entity->getScope(){{/scopeField}});
}

while (true) {
if (!isset($slugs[$slug])) {
$slugs[$slug] = $entity;
break;
}

//create another slug
$slug = $incrementSlug($slug{{#pattern}}, '/'{{/pattern}});
}
$slugs = [];

$slugs[$slug] = $entity;
}
foreach ($event->getEntities() as $entity) {
$slugModified = true;
if (!$this->getSession()->isNew($entity)) {
//update, so check if field has been changed.
$slugModified = $this->getEntityMap()->isFieldModified($entity, $slugField);
//updates
foreach ($event->getEntitiesToUpdate() as $entity) {
$slugModified = $this->getEntityMap()->isFieldModified($entity, $slugField);
{{#notPermanent}}
if ($this->getEntityMap()->isFieldModified($entity, $primaryStringField) && !$slugModified) {
$entity->setSlug($createSlug($entity));
{{#notPermanent}}
if ($this->getEntityMap()->isFieldModified($entity, $primaryStringField) && !$slugModified) {
$entity->setSlug($createSlug($entity));
}
{{/notPermanent}}
}
{{/notPermanent}}

if ($slugModified) {
$slug = $entity->getSlug();
Expand Down
3 changes: 2 additions & 1 deletion src/Propel/Generator/Builder/Om/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ public function applyBehaviorHooks($hookName)
$hookBehaviorMethodName = $hookName . ucfirst(NamingTool::toCamelCase($behavior->getId()));

if ($code) {
$body .= "//behavior hook {$behavior->getName()}#{$behavior->getId()}\n";
$body .= "\n//behavior hook {$behavior->getName()}#{$behavior->getId()}";

$method = new PhpMethod($hookBehaviorMethodName);
$method->setVisibility('protected');
$method->addSimpleParameter('event');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@ public function process()
$onDelete = $relation->hasOnDelete() ? "'" . $relation->getOnDelete() . "'" : 'null';
$onUpdate = $relation->hasOnUpdate() ? "'" . $relation->getOnUpdate() . "'" : 'null';

$type = "RelationMap::ONE_TO_" . ($relation->isLocalPrimaryKey() ? "ONE" : "MANY");

$pluralName = $relation->isLocalPrimaryKey() ? 'null' : var_export($this->getRefRelationVarName($relation, true), true);

$body .= "
//ref relation
\$this->addRelation($relationName, $target, RelationMap::ONE_TO_" . ($relation->isLocalPrimaryKey(
) ? "ONE" : "MANY") . ", $columnMapping, $onDelete, $onUpdate";
if ($relation->isLocalPrimaryKey()) {
$body .= ");";
} else {
$body .= ", '" . $this->getRefRelationVarName($relation, true) . "');";
}
\$this->addRelation($relationName, $target, $type, $columnMapping, $onDelete, $onUpdate, $pluralName);";
}

foreach ($this->getEntity()->getCrossRelations() as $crossRelation) {
$relationName = var_export($this->getCrossRelationVarName($crossRelation), true);
$pluralName = var_export($this->getCrossRelationVarName($crossRelation), true);

$relation = $crossRelation->getOutgoingRelation();
$relationName = var_export($this->getRelationVarName($relation, true), true);
$pluralName = $relationName;

$target = var_export($crossRelation->getForeignEntity()->getFullClassName(), true);

$onDelete = $crossRelation->getIncomingRelation()->hasOnDelete() ? "'" . $crossRelation->getIncomingRelation()->getOnDelete() . "'" : 'null';
Expand All @@ -78,9 +79,10 @@ public function process()
];

$mapping = var_export($mapping, true);
$polymorphic = var_export($crossRelation->isPolymorphic(), true);
$body .= "
//cross relation
\$this->addRelation($relationName, $target, RelationMap::MANY_TO_MANY, $mapping, $onDelete, $onUpdate, $pluralName);";
\$this->addRelation($relationName, $target, RelationMap::MANY_TO_MANY, $mapping, $onDelete, $onUpdate, $pluralName, $polymorphic);";
}

$this->addMethod('buildRelations')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,32 @@ public function process()
}

foreach ($this->getEntity()->getCrossRelations() as $crossRelation) {
$varName = $this->getRefRelationCollVarName($crossRelation->getIncomingRelation());
$varName = $this->getRelationVarName($crossRelation->getOutgoingRelation(), true);

$to = [];
foreach ($crossRelation->getRelations() as $relation) {
$to[] = $relation->getForeignEntity()->getFullClassName();
}
$to = implode(', ', $to);
$to = $crossRelation->getOutgoingRelation()->getForeignEntity()->getFullClassName();

$body .= "
// cross relation {$crossRelation->getMiddleEntity()->getFullClassName()} (to $to)
if (\$isset(\$entity, '$varName') && \$relationEntities = \$reader(\$entity, '$varName')) {
foreach (\$relationEntities as \$relationEntity) {
\$session->persist(\$relationEntity, \$deep);
";
if ($crossRelation->isPolymorphic()) {
foreach ($crossRelation->getRelations() as $idx => $relation) {
$class = '\\' . $relation->getForeignEntity()->getFullClassName();
$body .= "
//$idx is from type $class
if (!isset(\$relationEntity[$idx]) || !(\$relationEntity[$idx] instanceof $class)) {
throw new \\UnexpectedValueException('In ObjectCombinationCollection the $idx needs to be $class');
}
\$session->persist(\$relationEntity[$idx], \$deep);";
}

} else {
$body .= "
\$session->persist(\$relationEntity, \$deep);";
}

$body .= "
}
}
";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,18 @@ protected function addCrossRelationCount(CrossRelation $crossRelation)
EOF;

$body = <<<EOF
\$partial = \$this->{$collName}Partial && !\$this->isNew();
if (null === \$this->$collName || null !== \$criteria || \$partial) {
if (\$this->isNew() && null === \$this->$collName) {
return 0;
} else {
if (\$partial && !\$criteria) {
return count(\$this->get$relatedName());
}
\$query = $relatedQueryClassName::create(null, \$criteria);
if (\$distinct) {
\$query->distinct();
}
return \$query
->filterBy{$selfRelationName}(\$this)
->count();
}
} else {
if (func_num_args() === 0 || \$this->isNew()) {
return count(\$this->$collName);
}
\$query = $relatedQueryClassName::create(null, \$criteria);
if (\$distinct) {
\$query->distinct();
}
return \$query
->filterBy{$selfRelationName}(\$this)
->count();
EOF;


Expand Down
Loading

0 comments on commit e8e0db9

Please sign in to comment.