Author: david
Date: Mon Dec 6 17:10:28 2010
New Revision: 8876
Log:
Try up to 10 random slugs to avoid collisions on insert.
Modified:
trunk/lib/model/QubitObject.php
trunk/lib/model/QubitSlug.php
Modified: trunk/lib/model/QubitObject.php
==============================================================================
--- trunk/lib/model/QubitObject.php Mon Dec 6 16:38:27 2010 (r8875)
+++ trunk/lib/model/QubitObject.php Mon Dec 6 17:10:28 2010 (r8876)
@@ -209,7 +209,7 @@
if (1 > strlen($this->slug))
{
- $statement->execute(array($this->id, QubitSlug::random()));
+ $statement->execute(array($this->id,
QubitSlug::getUnique($connection)));
return $this;
}
@@ -222,7 +222,7 @@
// Collision? Try random, digit and letter slug
catch (PDOException $e)
{
- $statement->execute(array($this->id, QubitSlug::random()));
+ $statement->execute(array($this->id,
QubitSlug::getUnique($connection)));
}
}
Modified: trunk/lib/model/QubitSlug.php
==============================================================================
--- trunk/lib/model/QubitSlug.php Mon Dec 6 16:38:27 2010 (r8875)
+++ trunk/lib/model/QubitSlug.php Mon Dec 6 17:10:28 2010 (r8876)
@@ -65,4 +65,28 @@
return $slug;
}
+
+ public static function getUnique($connection = null)
+ {
+ if (!isset($connection))
+ {
+ $connection =
QubitTransactionFilter::getConnection(QubitObject::DATABASE_NAME);
+ }
+
+ // Try a max of 10 times before giving up (avoid infinite loops when
+ // possible slugs exhausted)
+ for ($i=0; $i < 10; $i++)
+ {
+ $slug = self::random();
+
+ $statement = $connection->prepare('
+ SELECT COUNT(*) FROM '.QubitSlug::TABLE_NAME.' WHERE
'.QubitSlug::SLUG.' = ?;');
+ $statement->execute(array($slug));
+
+ if (0 == $statement->fetchColumn(0))
+ {
+ return $slug;
+ }
+ }
+ }
}
--
You received this message because you are subscribed to the Google Groups
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/qubit-commits?hl=en.