How to handle DB-fields which are not in the form?

How to handle DB-fields which are not in the form?

logisticslogistics Posts: 6Questions: 2Answers: 0
edited May 2015 in Editor

Each of my tables contain the fields : createDate, modifiedDate, modifyUser, deleted.
I don't put these fields in my form and they shoud be modified in the following way:

  • NEW : createDate and modifiedDate are set to ToDay
  • EDIT : modifiedDate is set to ToDay
  • DELETE: modifiedDate is set to ToDay, deleted is set to 1

In all three situations modifyUser should be set to the id of the actual.

How can I arrange it, that the above rules are functioning?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Are you using the PHP or .NET libraries provided for Editor? If so, the set() method and also setValue() are what you are looking for here.

    For example you might have:

    Field::inst( 'createDate' )
      ->set( Field::SET_CREATE )
      ->setValue( date('Y-m-d') ),
    Field::inst( 'modifiedDate' ) // no need to use SET_BOTH - it is the default value
      ->setValue( date('Y-m-d') )
    

    The .NET libraries have similar options.

    The other option is to use your database to to it with triggers.

    Regards,
    Allan

  • logisticslogistics Posts: 6Questions: 2Answers: 0
    edited August 2015

    Hi Allen,

    Thanks, this solved my question for the fields "createDate" and "modifiedDate", however not the question for the field "deleted". I use a so called "soft"-delete, meaning that I put the row to deleted (by changing the value from 0 to 1) instead of really deleting the row.

    At the moment I do it by showing the field "deleted" so the user has to change this value to 1. However, it would be more natural for the user to use the "delete"-button. As far as I see, the result of the "delete"-button is now a hard delete, meaning the record will be removed from the table.

    Can you give me a glue or is my wish not possible at the moment?

    Regards, Niek.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    For a soft delete what you actually want to do is use an edit rather than the Editor delete command (since really you are just editing the row's data!).

    For this you would set up a separate Editor instance that would just set the value of the field that indicates the delete and submit that. You could have Editor show the confirm message (using message() if you wanted, or just have it immediately submit).

    Allan

  • logisticslogistics Posts: 6Questions: 2Answers: 0

    Hi Allen,

    In fact a soft delete is, at least with me, used quit a lot. I use it to keep info from the past for data-integrity.

    As you can see above, I solved it already in your manner. However it need two edit-situations and extra code (-> two js- and two php-programs) for quite a lot of my tables.

    Would it be possible for you to change the code as follows:

    1. If the "Delete"-button is activated, than a new field SET_DELETE is set to true like now is done with "Create"=button and SET_CREATE.
    2. If in the server-script at least one field has : ->set( Field::SET_DELETE ) than no hard delete is done and the actions are left to the developer.
    3. If in the server-script no field has : ->set( Field::SET_DELETE ) than a hard delete is done.

    Regards, Niek.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Thanks for the suggestion - I'll certainly look into this as I like soft deletes myself. It won't be something that will make it into Editor 1.5, but I've added it to my list of features for future versions.

    Regards,
    Allan

This discussion has been closed.