Pods::save PHP Method

save() public method

Though this function has the capacity to add new items, best practice should direct you to use add() for that instead.
See also: PodsAPI::save_pod_item
Since: 2.0
public save ( array | string $data = null, mixed $value = null, integer $id = null, array $params = null ) : integer
$data array | string Either an associative array of field information or a field name
$value mixed (optional) Value of the field, if $data is a field name
$id integer (optional) ID of the pod item to update
$params array (optional) Additional params to send to save_pod_item
return integer The item ID
    public function save($data = null, $value = null, $id = null, $params = null)
    {
        if (null !== $value) {
            $data = array($data => $value);
        }
        $fetch = false;
        if (null === $id || $this->row && $id == $this->id()) {
            $fetch = true;
            if (null === $id) {
                $id = $this->id();
            }
        }
        $data = (array) $this->do_hook('save', $data, $id);
        if (empty($data)) {
            return $id;
        }
        $default = array();
        if (!empty($params) && is_array($params)) {
            $default = $params;
        }
        $params = array('pod' => $this->pod, 'id' => $id, 'data' => $data, 'allow_custom_fields' => true, 'clear_slug_cache' => false);
        if (!empty($default)) {
            $params = array_merge($params, $default);
        }
        $id = $this->api->save_pod_item($params);
        if (0 < $id && $fetch) {
            $this->fetch($id, false);
        }
        if (!empty($this->pod_data['field_slug'])) {
            if (0 < $id && $fetch) {
                $slug = $this->field($this->pod_data['field_slug']);
            } else {
                $slug = pods($this->pod, $id)->field($this->pod_data['field_slug']);
            }
            if (0 < strlen($slug)) {
                pods_cache_clear($slug, 'pods_items_' . $this->pod);
            }
        }
        return $id;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Save
  */
 protected function save()
 {
     $this->ID = $this->pod->save($this->data);
 }