0

Anyone know the proper way to get data stored in a matrix row from the entry_submission_end hook for use in an addon?

2 Answers 2

1

One solution I've come up with is to pull the rows directly out of the database. This works but seems like there should be an api or hook to do this still in the event the Matrix code gets refactored. I can also see that it's an unnecessary database query since you are saving the data then re-accessing it.

Below is an example with some hardcoded Matrix col_id and field_id values.

public function entry_submission_end($entry_id, $meta, $data)
{   
    $query = ee()->db->from('matrix_data')
        ->select('col_id_362 status, col_id_363 email')
        ->where('field_id', 911)
        ->where('entry_id', $entry_id);

    $result = $query->get()->result();

    foreach ($result as $row) 
    {
        error_log($row->status);
    }
}
0

I guess your starting point should be print_r the array of submitted data, there you can see what field_ids are submitted for Matrix. After that you can get specific field something like this $obj[...][...]. I would also like to know if there is more "nicer" way of doing this.

edit: I added my version of code based on yours

public function entry_submission_end($entry_id, $meta, $data)
{   
    //Just for test to see the field_id, you can check this in DB also
    // image 1
    print_r($data); //this will output array of submitted data

    //now that we know our matrix field, lets get it unserialized
    $matrix_field = unserialize(base64_decode($data["revision_post"]["field_id_3"]));

    //lets print to see what we will get
    print_r($matrix_field); //this will give result image 2

    // $matrix_field now contains our matrix array, so you can use it...
}

image 1: http://cl.ly/image/381i0k0v3C18

image 2: http://cl.ly/image/0V2A3F0x2Z3o

If you need to manipulate matrix data, don't forget to use base64_encode(serialize($your_new_matrix_data)); to get it back to initial format.

I'm really new to all this, so maybe some add-on developer could provide better solution, but I think there is no need you to make another query to get data that are already there.

let me know if it helps

4
  • The submitted data doesn't store anything it seems. Are you saying there's a different way to fetch the data from the data array? Jul 4, 2013 at 8:38
  • Hm, are you sure there is no data? I replicated your situation and I updated my answer. Check if it is ok.
    – Davor Peic
    Jul 4, 2013 at 13:08
  • Yea I get nothing in the array for that field_id. At first I was thinking it was because it was an existing row but maybe it's because it's an existing entry. Jul 4, 2013 at 17:12
  • so you see the array? can you take a screenshot as I did?
    – Davor Peic
    Jul 4, 2013 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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