Snippets

Trevor Sacks Neutered delete() function

Updated by Trevor Sacks

File Problem Blocks.php Added

  • Ignore whitespace
  • Hide word diff
+...
+
+$this->freeze();
+foreach($this->relations() as $rel_name => $rel)
+{
+    $should_cascade = is_array($cascade) ? in_array($rel_name, $cascade) : $rel->cascade_delete;
+
+    // Give model subclasses a chance to chip in.
+    if ($should_cascade && ! $this->should_cascade_delete($rel))
+    {
+        // The function returned false so something does not want this relation to be cascade deleted
+        $should_cascade = false;
+    }
+
+    $rel->delete($this, $this->{$rel_name}, false, $should_cascade);
+}
+$this->unfreeze();
+
+...
+
+$this->freeze();
+foreach($this->relations() as $rel_name => $rel)
+{
+    $should_cascade = is_array($cascade) ? in_array($rel_name, $cascade) : $rel->cascade_delete;
+
+    // Give model subclasses a chance to chip in.
+    if ($should_cascade && ! $this->should_cascade_delete($rel))
+    {
+        // The function returned false so something does not want this relation to be cascade deleted
+        $should_cascade = false;
+    }
+
+    $rel->delete($this, $this->{$rel_name}, true, $should_cascade);
+}
+$this->unfreeze();
+
+...
+
+foreach($this->relations() as $rel_name => $rel)
+{
+    $this->_original_relations[$rel_name] = $rel->singular ? null : array();
+}
+
+...
Created by Trevor Sacks

File Badge.php Added

  • Ignore whitespace
  • Hide word diff
+class Model_Badge extends \Orm\Model_Soft
+{
+
+...
+
+public function delete($cascade = null, $use_transaction = false)
+	{
+		// New objects can't be deleted, neither can frozen
+		if ($this->is_new() or $this->frozen())
+		{
+			return false;
+		}
+
+		if ($use_transaction)
+		{
+			$db = \Database_Connection::instance(static::connection(true));
+			$db->start_transaction();
+		}
+
+		try
+		{
+			$this->observe('before_delete');
+
+			// Delete the model in question
+			if ( ! $this->delete_self())
+			{
+				return false;
+			}
+
+			// Perform cleanup:
+			// remove from internal object cache, remove PK's, set to non saved object, remove db original values
+			if (array_key_exists(get_called_class(), static::$_cached_objects)
+				and array_key_exists(static::implode_pk($this), static::$_cached_objects[get_called_class()]))
+			{
+				unset(static::$_cached_objects[get_called_class()][static::implode_pk($this)]);
+			}
+			foreach ($this->primary_key() as $pk)
+			{
+				unset($this->_data[$pk]);
+			}
+
+			$this->_is_new = true;
+			$this->_original = array();
+
+
+			$this->observe('after_delete');
+
+			$use_transaction and $db->commit_transaction();
+		}
+		catch (\Exception $e)
+		{
+			$use_transaction and $db->rollback_transaction();
+			throw $e;
+		}
+
+		return $this;
+	}
+
+...
+
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.