Git Product home page Git Product logo

Comments (10)

ah8r avatar ah8r commented on May 27, 2024 1

Yes, please if you could let me know how to do it manually that would be really helpful.

from audit-stash.

lorenzo avatar lorenzo commented on May 27, 2024

No, it does not. It is one of many missing features. You can force it to do it manually, though. Let me know if you need help getting that to work

from audit-stash.

aceat64 avatar aceat64 commented on May 27, 2024

I'm also running into this issue, being able to at least force it manually would be good.

from audit-stash.

lorenzo avatar lorenzo commented on May 27, 2024

@aceat64 If I tell you how to do it would you volunteer for documenting it?

from audit-stash.

aceat64 avatar aceat64 commented on May 27, 2024

Sure

from audit-stash.

lorenzo avatar lorenzo commented on May 27, 2024

@aceat64 This is what I personally do:

<?php
namespace App\Model\Audit;
use Cake\Utility\Text;
use SplObjectStorage;
class AuditTrail
{
    protected $_auditQueue;
    protected $_auditTransaction;
    public function __construct()
    {
        $this->_auditQueue = new SplObjectStorage;
        $this->_auditTransaction = Text::uuid();
    }
    public function toSaveOptions()
    {
        return [
            '_auditQueue' => $this->_auditQueue,
            '_auditTransaction' => $this->_auditTransaction
        ];
    }
}
$trail = new AuditTrail();
$success = $connection->transactional(function () use ($trail) {

    $table1->save($data1, $trail->toSaveOptions());
    $table2->save($data2, $trail->toSaveOptions());
    ....
    $tableN->save($dataN, $trail->toSaveOptions());

    return true;
});

if ($success) {
    $event = new Event('Model.afterCommit', $table);
    $table->behaviors()->get('AuditLog')->afterCommit($event, $result, $auditTrail->toSaveOptions());
}

from audit-stash.

aceat64 avatar aceat64 commented on May 27, 2024

@lorenzo I'm running into some issues and could use a bit more info. Where is $result coming from? Do I need to run the event for each table?

from audit-stash.

lorenzo avatar lorenzo commented on May 27, 2024

You run the event in only one table, typically, if you are saving associations, you call it on the parent.

$result needs to be an entity, as that is the signature for the afterCommit event. It does not really matter what entity, but I pass the parent entity when saving associations.

from audit-stash.

kevinquinnyo avatar kevinquinnyo commented on May 27, 2024

@lorenzo How hard would it be to make transactional saves work natively with this plugin, without the additional hacks above?

My concern is that we as developers can forget to add the code above in certain situations and we will be missing the audit logs.

Do you already have some ideas on how this could be achieved out-of-the-box? If so, I don't mind taking a stab at it, but I would probably need some guidance.

I know you're busy, so thanks for taking the time if you can.

from audit-stash.

lorenzo avatar lorenzo commented on May 27, 2024

The only way I can think of is by having some sort of global state somewhere tracking whether or not a transaction has started in the connection, and then the behaviour would inject the global shared audit state to the save() method

Developers would still need to wrap their calls in transactional() though

from audit-stash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.