Example #1
0
 public function get($objectname = false)
 {
     if ($objectname && !$this->structure && !($this->structure = FW4_Structure::get_object_structure($this->objectname_prefix . $objectname))) {
         return false;
     }
     if (!count($this->order_by)) {
         if (isset($this->structure['order'])) {
             $this->order_by(strval($this->structure['order']));
         } else {
             if (isset($this->structure['sortable']) && $this->structure['sortable']) {
                 $this->order_by('_sort_order, id DESC');
             }
         }
     }
     $translate = array();
     if ($this->structure) {
         // A page always needs a record
         if ($this->structure->getName() == 'page' && !count($this->where)) {
             $force_entry = true;
         } else {
             $force_entry = false;
         }
         $types = FW4_Type_Manager::get_instance();
         foreach ($this->structure->children() as $child) {
             if ($child->getName() == 'object' && !isset($this->children[strval($child['name'])])) {
                 $this->children[strval($child['name'])] = true;
             } else {
                 if ($this->translate && isset($child['translatable']) && $child['translatable'] && $child['translatable'] != 'false') {
                     if ($child->getName() == 'slug' && !isset($child['name'])) {
                         $child['name'] = 'slug';
                     }
                     $translate[strval($child['name'])] = strval($child['name']) . '_' . language();
                 } else {
                     if ($child->getName() == 'dbrelation' && !isset($this->relations[strval($child['name'])])) {
                         $this->relations[strval($child['name'])] = current($child->attributes());
                     } else {
                         if (count($this->select) == 0 && isset($child['type_name']) && ($type_obj = $types->get_type(strval($child['type_name'])))) {
                             if (method_exists($type_obj, 'on_fetch')) {
                                 $this->fetchable_fields[] = array('type' => $type_obj, 'field' => new SimpleXMLElement($child->asXML()));
                             }
                         } else {
                             if ($child->getName() == 'recursive') {
                                 if (count($this->where) == 0) {
                                     $this->where('parent_id IS NULL');
                                 }
                                 $this->relations[strval($child['name'])] = array('local_key' => 'id', 'foreign_key' => 'parent_id', 'source' => strval($this->structure['stack']));
                             }
                         }
                     }
                 }
             }
         }
         if (isset($this->structure['recursive'])) {
             $this->relations[strval($this->structure['name'])] = array('local_key' => 'id', 'foreign_key' => 'parent_id', 'source' => strval($this->structure['stack']));
             if (isset($this->structure['order'])) {
                 $this->relations[strval($this->structure['name'])]['order'] = strval($this->structure['order']);
             }
         }
     }
     if (isset($this->structure['model']) && !count($this->select) && (class_exists(ucfirst($this->structure['model'])) || file_exists(CONTENTPATH . Router::get_content_prefix() . strtolower($this->structure['contentname']) . '/models/' . strtolower($this->structure['model']) . ".php"))) {
         if (!class_exists(ucfirst($this->structure['model']))) {
             include_once CONTENTPATH . Router::get_content_prefix() . strtolower($this->structure['contentname']) . '/models/' . strtolower($this->structure['model']) . ".php";
         }
         if (class_exists(ucfirst($this->structure['model']))) {
             $this->model_name = ucfirst($this->structure['model']);
         }
     } else {
         if ($this->model_name != 'Model') {
             if (!class_exists($this->model_name)) {
                 include_once CONTENTPATH . Router::get_content_prefix() . strtolower($this->structure['contentname']) . '/models/' . strtolower($this->model_name) . ".php";
             }
         }
     }
     $sql = $this->get_sql();
     if ($this->debug) {
         var_dump($sql);
     }
     $start = microtime(true);
     $result = new Resultset($this->db->query($sql, PDO::FETCH_CLASS, $this->model_name, array('translate' => $translate)), $this->structure ? strval($this->structure['stack']) : $this->from, $this->structure ? strval($this->structure['name']) : $objectname, !isset($this->structure['child']));
     $time = microtime(true) - $start;
     FW4_Db::$query_log[$sql] = round($time * 1000, 3);
     $result->_children($this->children);
     $result->_relations($this->relations);
     $result->_fetchable_fields($this->fetchable_fields);
     if ($this->structure && $force_entry && $result->rowCount() == 0) {
         if ($this->structure['parent_type'] == 'site') {
             $site = current_site();
             $id = insert($objectname, array('site_id' => $site['id']));
         } else {
             $id = insert($objectname, array());
         }
         return $this->get($objectname);
     }
     return $result;
 }