How can i excule a row from changing anothers rows value when updating it

How can i excule a row from changing anothers rows value when updating it

Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1

Currenty, each time a row is updated I store teh current timestpamp to keep a record. But, for a particulat column i dont want it to update the time. How can I achieve this

This is how initially it is

  Field::inst('OL.followOn', 'followOn')


      Field::inst('OL.updatedOn', 'updatedOn')
            ->set(true)
            ->setValue(date("Y-m-d H:i:s")),

This is another appraoch I tried but didn't seem to work


- Field::inst('OL.updatedOn', 'updatedOn') ->set(false), // ->setValue(date("Y-m-d H:i:s")), ->on('postEdit', function ($editor, $values) { $excludeColumn = 'followOn'; $shouldUpdate = true; if (isset($values['data']) && is_array($values['data'])) { foreach ($values['data'] as $key => $row) { if (isset($row[$excludeColumn]) && count($row) === 1) { $shouldUpdate = false; } } } else { $shouldUpdate = false; } if ($shouldUpdate) { $editor->field('updatedOn') ->setValue(date("Y-m-d H:i:s")); } else { $editor->field('updatedOn') ->set(false); } })

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin

    Doing it in postEdit would be too late - it has already been written. Have you tried your code in preEdit?

    Allan

  • Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1

    yes, I have tried it but wont work. How could I get the value of Follow On.

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin

    I would have expected that to work. Can you show the code you were using? Also, it looks like your parameters for the function at a little wrong (for postEdit or for preEdit).

    As the documentation notes, both get $editor, $id, $values parameters, and $values is not the full HTTP object. It is the values for that row only. So you could use $values['followOn'].

    Allan

  • rf1234rf1234 Posts: 2,992Questions: 87Answers: 421

    In addition to Allan's remarks: Have you set the form options to "submit: 'changed'"?
    https://editor.datatables.net/reference/type/form-options#submit

    If not, all columns will be submitted to the server anyway. And you logic won't work.

    The default for form editor is "submit: 'all'". So you would have to change this.
    https://editor.datatables.net/reference/option/formOptions.main

  • Aryan1703Aryan1703 Posts: 77Questions: 20Answers: 1

    Instead of that I used $_POST to get the values as $values was only giving me the row id. That helped me


    ->on('preEdit', function ($editor, $id, &$values) { global $prevValues, $initialUser; getPrevValues($editor->db(), $editor->table()[0], $id); $previousValue = $prevValues[$id]; $initialUser = $previousValue['updatedByEdit']; error_log("previous user" . $initialUser); foreach ($_POST['data'] as $rowKey => $rowData) { $followOnValue = isset($rowData['followOn']) ? true : false; if ($followOnValue) { } else { $editor->field('updatedOn') ->set(true) ->setValue(date("Y-m-d H:i:s")); error_log("Updating 'updatedOn' and 'updatedByEdit'."); } } })
  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Yup that will do it as well. Nice one. Problem solved then?

Sign In or Register to comment.