]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Managed_DataObject.php
styling fixes to primary aside section
[quix0rs-gnu-social.git] / classes / Managed_DataObject.php
index ae37dd02eaa911f5846bc47f068c144fd31244a9..531232332dd24e0889bf1f61250db7ac79e12976 100644 (file)
@@ -111,7 +111,7 @@ abstract class Managed_DataObject extends Memcached_DataObject
     }
 
     /**
-     * Get a multi-instance object in an array
+     * Get a multi-instance object separated into an array
      *
      * This is a utility method to get multiple instances with a given set of
      * values for a specific key column. Usually used for the primary key when
@@ -134,7 +134,7 @@ abstract class Managed_DataObject extends Memcached_DataObject
      * @access public
      * @return array (associative)
      */
-    function table()
+    public function table()
     {
         $table = static::schemaDef();
         return array_map(array($this, 'columnBitmap'), $table['fields']);
@@ -164,7 +164,7 @@ abstract class Managed_DataObject extends Memcached_DataObject
 
     function sequenceKey()
     {
-        $table = call_user_func(array(get_class($this), 'schemaDef'));
+        $table = static::schemaDef();
         foreach ($table['fields'] as $name => $column) {
             if ($column['type'] == 'serial') {
                 // We have a serial/autoincrement column.
@@ -188,7 +188,7 @@ abstract class Managed_DataObject extends Memcached_DataObject
 
     function keyTypes()
     {
-        $table = call_user_func(array(get_class($this), 'schemaDef'));
+        $table = static::schemaDef();
         $keys = array();
 
         if (!empty($table['unique keys'])) {
@@ -253,7 +253,7 @@ abstract class Managed_DataObject extends Memcached_DataObject
     {
         $links = array();
 
-        $table = call_user_func(array(get_class($this), 'schemaDef'));
+        $table = static::schemaDef();
 
         foreach ($table['foreign keys'] as $keyname => $keydef) {
             if (count($keydef) == 2 && is_string($keydef[0]) && is_array($keydef[1]) && count($keydef[1]) == 1) {
@@ -274,7 +274,7 @@ abstract class Managed_DataObject extends Memcached_DataObject
      */
     function _allCacheKeys()
     {
-        $table = call_user_func(array(get_class($this), 'schemaDef'));
+        $table = static::schemaDef();
         $ckeys = array();
 
         if (!empty($table['unique keys'])) {
@@ -298,4 +298,26 @@ abstract class Managed_DataObject extends Memcached_DataObject
         }
         return $ckeys;
     }
+
+    /**
+     * Returns an ID, checked that it is set and reasonably valid
+     *
+     * If this dataobject uses a special id field (not 'id'), just
+     * implement your ID getting method in the child class.
+     *
+     * @return int ID of dataobject
+     * @throws Exception (when ID is not available or not set yet)
+     */
+    public function getID()
+    {
+        // FIXME: Make these exceptions more specific (their own classes)
+        if (!isset($this->id)) {
+            throw new Exception('No ID set.');
+        } elseif (empty($this->id)) {
+            throw new Exception('Empty ID for object! (not inserted yet?).');
+        }
+
+        // FIXME: How about forcing to return an int? Or will that overflow eventually?
+        return $this->id;
+    }
 }