Skip to content

Commit

Permalink
Fix object without pattern
Browse files Browse the repository at this point in the history
Sorry, can't launch test myself atm.
  • Loading branch information
Divi committed Aug 16, 2012
1 parent 226e460 commit ac39007
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions generator/lib/behavior/sluggable/SluggableBehavior.php
Expand Up @@ -79,11 +79,11 @@ protected function getColumnSetter()
public function preSave($builder) public function preSave($builder)
{ {
$const = $builder->getColumnConstant($this->getColumnForParameter('slug_column')); $const = $builder->getColumnConstant($this->getColumnForParameter('slug_column'));
$script = "
if (\$this->isColumnModified($const)";
$pattern = $this->getParameter('slug_pattern'); $pattern = $this->getParameter('slug_pattern');
if ($pattern && $this->getParameter('permanent') == 'false') {
$script .= " || "; if ($pattern && $this->getParameter('permanent') != 'true') {
$script = "
if ((\$this->isColumnModified($const) || ";
$count = preg_match_all('{[a-zA-Z]+}', $pattern, $matches, PREG_PATTERN_ORDER); $count = preg_match_all('{[a-zA-Z]+}', $pattern, $matches, PREG_PATTERN_ORDER);


foreach ($matches[0] as $key => $match) { foreach ($matches[0] as $key => $match) {
Expand All @@ -94,12 +94,28 @@ public function preSave($builder)
$columnConst = $builder->getColumnConstant($column); $columnConst = $builder->getColumnConstant($column);
$script .= "\$this->isColumnModified($columnConst)" . ($key < $count - 1 ? " || " : ""); $script .= "\$this->isColumnModified($columnConst)" . ($key < $count - 1 ? " || " : "");
} }

$script .= ") && \$this->{$this->getColumnGetter()}()) {";
}
else {
$script .= "if (\$this->isColumnModified($const) && \$this->{$this->getColumnGetter()}()) {";
} }
$script .= " && \$this->{$this->getColumnGetter()}()) {
\$this->{$this->getColumnSetter()}(\$this->makeSlugUnique(\$this->{$this->getColumnGetter()}())); $script .= "
\$this->{$this->getColumnSetter()}(\$this->makeSlugUnique(\$this->{$this->getColumnGetter()}()));";

if (null == $pattern && $this->getParameter('permanent') != 'true') {
$script .= "
} else {
\$this->{$this->getColumnSetter()}(\$this->createSlug());
}";
}
else {
$script .= "
} elseif (!\$this->{$this->getColumnGetter()}()) { } elseif (!\$this->{$this->getColumnGetter()}()) {
\$this->{$this->getColumnSetter()}(\$this->createSlug()); \$this->{$this->getColumnSetter()}(\$this->createSlug());
}"; }";
}
return $script; return $script;
} }


Expand Down Expand Up @@ -292,7 +308,7 @@ protected function makeSlugUnique(\$slug, \$separator = '" . $this->getParameter
->count(); ->count();
if (1 == \$count) { if (1 == \$count) {
return $getter(); return \$this->$getter();
}"; }";
} }


Expand Down

0 comments on commit ac39007

Please sign in to comment.