Example #1
0
 public static function singleton($o, $id = NULL, $class = NULL)
 {
     if (!self::$active) {
         return $o;
     }
     if (!is_object($o) || !$o instanceof Model) {
         throw new Exception("trying to get singleton of non-Model instance");
     }
     if (!isset($class)) {
         $class = get_class($o);
     }
     if (Model::_has_complex_primary_key($class)) {
         return $o;
     }
     if (!isset($id)) {
         $PK = Model::_simple_primary_key($o);
         $id = $o->{$PK};
     }
     if ($x = ObjectCache::get($class, $id)) {
         return $x;
     } else {
         ObjectCache::store($class, $id, $o);
         return $o;
     }
 }
 public function object_($class = NULL, $pk = NULL)
 {
     if (isset($_GET['path'])) {
         self::$inspector_path = base64_decode($_GET['path']);
     } else {
         self::$inspector_path = '$' . $class;
     }
     $modelinfo = Model::modelinfo($class);
     if (Model::_has_complex_primary_key($modelinfo)) {
         throw new HTTPException("{$class} has complex primary key");
     }
     $pkcol = Model::_simple_primary_key($class);
     $obj = Model::_find_first($class, array($pk));
     if (!is_object($obj)) {
         throw new HTTPNotFound();
     }
     # WARNING: assumes integer primary keys with the lt and gt checks
     # need some other way to do navigation in the case of non-integer PKs
     $prev = Model::_find($class, array(array($pkcol => cond::lt($obj->{$pkcol})), 1, "{$pkcol} desc"));
     if ($prev) {
         $prev = array_shift($prev);
         $this->view->assign("prevobjid", $prev->id);
         $prev = url($this, 'object', $class, $prev->id);
         $this->view->assign("prevobjlink", $prev);
     }
     $next = Model::_find($class, array(array($pkcol => cond::gt($obj->{$pkcol})), 1, $pkcol));
     if ($next) {
         $next = array_shift($next);
         $this->view->assign("nextobjid", $next->id);
         $next = url($this, 'object', $class, $next->id);
         $this->view->assign("nextobjlink", $next);
     }
     $this->view->assign('class', $class);
     $this->view->assign('obj', $obj);
     $this->view->assign('pkcol', $pkcol);
     $this->view->assign('pk', $pk);
     $f = array_keys($obj->dump());
     usort($f, array($this, 'sortfields'));
     $this->view->assign('fields', $f);
     $f = $obj->natural_members();
     usort($f, array($this, 'sortfields'));
     $this->view->assign('natmembers', $f);
     $f = $obj->virtual_members();
     usort($f, array($this, 'sortfields'));
     $this->view->assign('virtmembers', $f);
     $f = $obj->generated_members();
     $this->view->assign('genfields', $f);
     $this->view->assign('path', self::$inspector_path);
     $this->viewname = "object.tpl";
 }
 public function dump($deep = false)
 {
     # $deep is unimplemented
     $r = array('collection-of' => $this->ofclass);
     $complexkey = Model::_has_complex_primary_key($this->ofclass);
     foreach ($this as $v) {
         $x = $v->pp();
         if ($complexkey) {
             # no really good thing to do here
             $r[] = $x;
         } else {
             $k = $v->_simple_primary_key();
             $r[$v->{$k}] = $x;
         }
     }
     return $r;
 }