1

I am trying to create a fieldtype that requires comparison to it's previous data. Any idea how to get this data in the save() function? I tried using $this->field_data but that just returns the "new" data submitted to the form.

To provide a tangible example, what I am trying to do is take the prior data and store it with the new data as an array so my save() function looks like this (if $this->field_data worked this should have):

function save($data) 
{ 
    if (!empty($this->field_data)) $data = json_decode($this->field_data, TRUE);
    $data[] = $data;


    return json_encode($data); 
}
1

This solution uses the channel_entries model:

function save($data) 
{
    if (ee()->input->post('entry_id'))
    {
        // look up old data
        ee()->load->model('channel_entries_model');
        $query = $this->EE->channel_entries_model
            ->get_entry(
                ee()->input->post('entry_id'), 
                ee()->input->post('channel_id')
            );
        $result = $query->row_array();

        // if exists set old data 
        if ($query->num_rows() > 0) $return = json_decode($result[$this->field_name], TRUE);

        // append new data
        $return[] = $data;

        return json_encode($return);
    }

    return "";
}
0

The only way to get the old data is to query for it:

//are we saving a new entry or updating an existing?
$entry_id = ee()->input->get('entry_id');

if ($entry_id)
{
  //get the old data from the database
  $query = ee()->db->select('field_id_'.$this->field_id)
                   ->where('entry_id', $entry_id)
                   ->get('channel_data');

  $old_data = $query->row('field_id_'.$this->field_id);

  $query->free_result();
}
1
  • Thanks Rob :) I'm on top of these things these days hah! I just wish there was a better solution sometimes... My solution is similar except I use the model and fieldtype object data to pull field_name so I'll post another answer as well below :) – spacesuitdiver Aug 16 '13 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.