PodsData::fetch PHP Method

fetch() public method

Fetch a new row for the current pod_data
Since: 2.0
public fetch ( integer $row = null, boolean $explicit_set = true ) : mixed
$row integer Row number to fetch
$explicit_set boolean Whether to set explicitly (use false when in loop)
return mixed
    public function fetch($row = null, $explicit_set = true)
    {
        global $wpdb;
        if (null === $row) {
            $explicit_set = false;
        }
        $already_cached = false;
        $id = $row;
        $tableless_field_types = PodsForm::tableless_field_types();
        if (null === $row) {
            $this->row_number++;
            $this->row = false;
            if (isset($this->data[$this->row_number])) {
                $this->row = get_object_vars($this->data[$this->row_number]);
                $current_row_id = false;
                if (in_array($this->pod_data['type'], array('post_type', 'media'))) {
                    $current_row_id = pods_var_raw('ID', $this->row);
                } elseif ('taxonomy' == $this->pod_data['type']) {
                    $current_row_id = pods_var_raw('term_id', $this->row);
                } elseif ('user' == $this->pod_data['type']) {
                    $current_row_id = pods_var_raw('ID', $this->row);
                } elseif ('comment' == $this->pod_data['type']) {
                    $current_row_id = pods_var_raw('comment_ID', $this->row);
                } elseif ('settings' == $this->pod_data['type']) {
                    $current_row_id = $this->pod_data['id'];
                }
                if (0 < $current_row_id) {
                    $row = $current_row_id;
                }
            }
        }
        if (null !== $row || 'settings' == $this->pod_data['type']) {
            if ($explicit_set) {
                $this->row_number = -1;
            }
            $mode = 'id';
            $id = pods_absint($row);
            if (!is_numeric($row) || 0 === strpos($row, '0') || $row != preg_replace('/[^0-9]/', '', $row)) {
                $mode = 'slug';
                $id = $row;
            }
            $row = false;
            if (!empty($this->pod)) {
                $row = pods_cache_get($id, 'pods_items_' . $this->pod);
                if (false !== $row) {
                    $already_cached = true;
                }
            }
            $current_row_id = false;
            $get_table_data = false;
            $old_row = $this->row;
            if (false !== $row && is_array($row)) {
                $this->row = $row;
            } elseif (in_array($this->pod_data['type'], array('post_type', 'media'))) {
                if ('post_type' == $this->pod_data['type']) {
                    if (empty($this->pod_data['object'])) {
                        $post_type = $this->pod_data['name'];
                    } else {
                        $post_type = $this->pod_data['object'];
                    }
                } else {
                    $post_type = 'attachment';
                }
                if ('id' == $mode) {
                    $this->row = get_post($id, ARRAY_A);
                    if (is_array($this->row) && $this->row['post_type'] != $post_type) {
                        $this->row = false;
                    }
                } else {
                    $args = array('post_type' => $post_type, 'name' => $id, 'numberposts' => 5);
                    $find = get_posts($args);
                    if (!empty($find)) {
                        $this->row = get_object_vars($find[0]);
                    }
                }
                if (is_wp_error($this->row) || empty($this->row)) {
                    $this->row = false;
                } else {
                    $current_row_id = $this->row['ID'];
                }
                $get_table_data = true;
            } elseif ('taxonomy' == $this->pod_data['type']) {
                $taxonomy = $this->pod_data['object'];
                if (empty($taxonomy)) {
                    $taxonomy = $this->pod_data['name'];
                }
                // Taxonomies are registered during init, so they aren't available before then
                if (!did_action('init')) {
                    // hackaround :(
                    if ('id' == $mode) {
                        $term_where = 't.term_id = %d';
                    } else {
                        $term_where = 't.slug = %s';
                    }
                    $filter = 'raw';
                    $term = $id;
                    if ('id' != $mode || !($_term = wp_cache_get($term, $taxonomy))) {
                        $_term = $wpdb->get_row($wpdb->prepare("SELECT t.*, tt.* FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND {$term_where} LIMIT 1", $taxonomy, $term));
                        if ($_term) {
                            wp_cache_add($term, $_term, $taxonomy);
                        }
                    }
                    $_term = apply_filters('get_term', $_term, $taxonomy);
                    $_term = apply_filters("get_{$taxonomy}", $_term, $taxonomy);
                    $_term = sanitize_term($_term, $taxonomy, $filter);
                    if (is_object($_term)) {
                        $this->row = get_object_vars($_term);
                    }
                } elseif ('id' == $mode) {
                    $this->row = get_term($id, $taxonomy, ARRAY_A);
                } else {
                    $this->row = get_term_by('slug', $id, $taxonomy, ARRAY_A);
                }
                if (is_wp_error($this->row) || empty($this->row)) {
                    $this->row = false;
                } else {
                    $current_row_id = $this->row['term_id'];
                }
                $get_table_data = true;
            } elseif ('user' == $this->pod_data['type']) {
                if ('id' == $mode) {
                    $this->row = get_userdata($id);
                } else {
                    $this->row = get_user_by('slug', $id);
                }
                if (is_wp_error($this->row) || empty($this->row)) {
                    $this->row = false;
                } else {
                    // Get other vars
                    $roles = $this->row->roles;
                    $caps = $this->row->caps;
                    $allcaps = $this->row->allcaps;
                    $this->row = get_object_vars($this->row->data);
                    // Set other vars
                    $this->row['roles'] = $roles;
                    $this->row['caps'] = $caps;
                    $this->row['allcaps'] = $allcaps;
                    unset($this->row['user_pass']);
                    $current_row_id = $this->row['ID'];
                }
                $get_table_data = true;
            } elseif ('comment' == $this->pod_data['type']) {
                $this->row = get_comment($id, ARRAY_A);
                // No slug handling here
                if (is_wp_error($this->row) || empty($this->row)) {
                    $this->row = false;
                } else {
                    $current_row_id = $this->row['comment_ID'];
                }
                $get_table_data = true;
            } elseif ('settings' == $this->pod_data['type']) {
                $this->row = array();
                if (empty($this->fields)) {
                    $this->row = false;
                } else {
                    foreach ($this->fields as $field) {
                        if (!in_array($field['type'], $tableless_field_types)) {
                            $this->row[$field['name']] = get_option($this->pod_data['name'] . '_' . $field['name'], null);
                        }
                    }
                    // Force ID
                    $this->id = $this->pod_data['id'];
                    $this->row['option_id'] = $this->id;
                }
            } else {
                $params = array('table' => $this->table, 'where' => "`t`.`{$this->field_id}` = " . (int) $id, 'orderby' => "`t`.`{$this->field_id}` DESC", 'page' => 1, 'limit' => 1, 'search' => false);
                if ('slug' == $mode && !empty($this->field_slug)) {
                    $id = pods_sanitize($id);
                    $params['where'] = "`t`.`{$this->field_slug}` = '{$id}'";
                }
                $this->row = pods_data()->select($params);
                if (empty($this->row)) {
                    $this->row = false;
                } else {
                    $current_row = (array) $this->row;
                    $this->row = get_object_vars((object) @current($current_row));
                }
            }
            if (!$explicit_set && !empty($this->row) && is_array($this->row) && !empty($old_row)) {
                $this->row = array_merge($old_row, $this->row);
            }
            if ('table' == $this->pod_data['storage'] && false !== $get_table_data && is_numeric($current_row_id)) {
                $params = array('table' => $wpdb->prefix . "pods_", 'where' => "`t`.`id` = {$current_row_id}", 'orderby' => "`t`.`id` DESC", 'page' => 1, 'limit' => 1, 'search' => false, 'strict' => true);
                if (empty($this->pod_data['object'])) {
                    $params['table'] .= $this->pod_data['name'];
                } else {
                    $params['table'] .= $this->pod_data['object'];
                }
                $row = pods_data()->select($params);
                if (!empty($row)) {
                    $current_row = (array) $row;
                    $row = get_object_vars((object) @current($current_row));
                    if (is_array($this->row) && !empty($this->row)) {
                        $this->row = array_merge($this->row, $row);
                    } else {
                        $this->row = $row;
                    }
                }
            }
            if (!empty($this->pod) && !$already_cached) {
                pods_cache_set($id, $this->row, 'pods_items_' . $this->pod, 0);
            }
        }
        $this->row = apply_filters('pods_data_fetch', $this->row, $id, $this->row_number, $this);
        return $this->row;
    }

Usage Example

 /**
  * @return array
  */
 public function get_row(&$counter = 0, $method = null)
 {
     if (!empty($this->row) && 0 < (int) $this->id && 'table' != $method) {
         return $this->row;
     }
     if (is_object($this->pod) && ('Pods' == get_class($this->pod) || 'Pod' == get_class($this->pod))) {
         $this->row = $this->pod->fetch();
     } else {
         $this->row = false;
         if (!empty($this->data)) {
             if (empty($this->data_keys) || count($this->data) != count($this->data_keys)) {
                 $this->data_keys = array_keys($this->data);
             }
             if (count($this->data) == $this->total && isset($this->data_keys[$counter]) && isset($this->data[$this->data_keys[$counter]])) {
                 $this->row = $this->data[$this->data_keys[$counter]];
                 $counter++;
             }
         }
         if (false === $this->row && 0 < (int) $this->id && !empty($this->sql['table'])) {
             $this->pods_data->select(array('table' => $this->sql['table'], 'where' => '`' . $this->sql['field_id'] . '` = ' . (int) $this->id, 'limit' => 1));
             $this->row = $this->pods_data->fetch();
         }
     }
     return $this->row;
 }
All Usage Examples Of PodsData::fetch