Footprint icon indicating copy to clipboard operation
Footprint copied to clipboard

FootprintBehavior's beforeSave doesn't seem to have the information it needs in it

Open Cylindric opened this issue 11 years ago • 28 comments

Hi. This isn't really an 'issue' so much as a query. I have, I think, followed the instructions for getting this set up, and if I put breakpoints in the FootprintListener and FootprintAwareTrait I can see the User data being set where it seems to need to be, however when it then reaches the beforeSave in the FootprintBehavior, $options doesn't seem to contain any data pertaining to the user.

My Table's initialise:

$this->addBehavior('Muffin/Footprint.Footprint', [
    'events' => [
        'Model.beforeSave' => [
            'created_by' => 'new',
            'modified_by' => 'always',
        ]
    ]
]);

The $options variable in the FootprintBehavior's beforeSave():

ArrayObject::__set_state(array(
   'atomic' => true,
   'associated' => 
  array (
    'characters' => 
    array (
    ),
    'createduser' => 
    array (
    ),
    'modifyuser' => 
    array (
    ),
  ),
   'checkRules' => true,
   'checkExisting' => true,
   '_primary' => true,
))

My full AppController My full XpTable

Cylindric avatar Sep 11 '15 15:09 Cylindric

Where is the save being called from?

jadb avatar Sep 12 '15 00:09 jadb

I have an XpController with an Add() method.

    public function add()
    {
        $xp = $this->Xp->newEntity();
        if ($this->request->is('post')) {
            $xp = $this->Xp->patchEntity($xp, $this->request->data);
            if ($this->Xp->save($xp)) {
                $response = ['result' => 'success', 'data' => $xp];
            } else {
                $response = ['result' => 'fail', 'data' => $xp];
            }
        }

        $this->set('response', $response);
        $this->set('_serialize', ['response']);
    }

My full XpController

Cylindric avatar Sep 14 '15 17:09 Cylindric

Weird.. Does your request data contain a user_id by any chance?

jadb avatar Sep 15 '15 06:09 jadb

Did you ever get this to work Cylindric? I noticed that you seem to be using https://github.com/ceeram/blame now. I'm having a similar issue.

waspinator avatar Oct 06 '15 18:10 waspinator

@waspinator do u have a failing test? Would love to get to the end of this.

jadb avatar Oct 06 '15 18:10 jadb

Sorry, yes I switched to Ceeram/blame just to get going. If I get time I'll try and spin up a test case, but I'm not massively familiar with tests for things like this, so it might take me a bit.

Cylindric avatar Oct 07 '15 08:10 Cylindric

@Cylindric It's ok if you can't provide a test case. You can try to debug the issue and provide us some specifics about what's causing the problem.

ADmad avatar Oct 07 '15 08:10 ADmad

I have the same issue, my options array in the FootprintBehaviour's beforeSave also looks weird:

object(ArrayObject) {
    atomic => true
    associated => [
        'projects' => [],
        'targetcompanies' => [],
        'users' => [],
        'cities' => [],
        'companiestarget' => [],
        'companies' => [],
        'contacttypes' => []
    ]
    checkRules => true
    checkExisting => true
    _primary => true
}

it doesn't contain any _footprint.id plus, if I debug current(Hash::extract((array)$options, $this->config('propertiesMap.' . $field))) it returns false....

kristiyandobrev avatar Jan 28 '16 18:01 kristiyandobrev

@kristiyandobrev please answer the following so we can better assist you:

  • what's the behavior's configuration like? (paste it please)
  • what are you trying to do, what's the code for it?
  • finally, what are you expecting?

jadb avatar Jan 28 '16 19:01 jadb

Alright.

I will start from the last question :smile_cat:

finally, what are you expecting?

In my table ContactsTable I have a called created_by_id (note that it's not created_by) which I want to set to the current (logged) user's id, every time I am creating a new database record.

what are you trying to do, what's the code for it?

For the purpose I first install your plugin from the command line like:

composer require muffin/footprint:dev-master

then I add it to my bootstrap.php

Plugin::load('Muffin/Footprint');

As it was required I included the Muffin\Footprint\Auth\FootprintAwareTrait to my AppController:

Link to my AppController

My AuthComponent is configured to use the default Users table which in my case is also called Users therefore I didn't find necessary to add $this->_userModel = 'SomeOtherTable'; in the beforeFilter.

what's the behavior's configuration like? (paste it please)

As I mentioned above I want to set the created_by_id of my every new record ContactsTable to be the current/logged user's id, therefore I am adding the following behaviour to my ContactsTable.

Link to my ContactsTable.php

To sum up, the above doesn't work for me. It inserts empty value for created_by_id in the ContactsTable.

I looked over the plugin, played a little bit with the debug option and what I found was that:

In FootprintListener

public function handleEvent(Event $event, $ormObject, $options)
    {
        $key = $this->config('optionKey');
        if (empty($options[$key]) && !empty($this->_currentUser)) {
            $options[$key] = $this->_currentUser;
        }
        debug($this->config('optionKey'));
    } 

The output of the above is actually a complete (not empty) entity of the logged user. But with further debugging I found that in FootprintBehaviour:

 public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
    {
        $eventName = $event->name();
        $events = $this->config('events');

        $new = $entity->isNew() !== false;

        foreach ($events[$eventName] as $field => $when) {
            if (!in_array($when, ['always', 'new', 'existing'])) {
                throw new UnexpectedValueException(
                    sprintf('When should be one of "always", "new" or "existing". The passed value "%s" is invalid', $when)
                );
            }

            if ($entity->dirty($field)) {
                continue;
            }

            if ($when === 'always' ||
                ($when === 'new' && $new) ||
                ($when === 'existing' && !$new)
            ) {
                $entity->set(
                    $field,
                    current(Hash::extract((array)$options, $this->config('propertiesMap.' . $field)))
                    debug(current(Hash::extract((array)$options, $this->config('propertiesMap.' . $field))));
                );
            }
        }
    }

The output of above debug is false and respectively I see empty value in my DB table (expects UUID not bool) which seems to be strange since the FootprintBehaviour I could actually see the entity that I need.

kristiyandobrev avatar Jan 28 '16 21:01 kristiyandobrev

Hmm, this all seems good.

You are saying in the behavior you could see the user entity, in which case it would be an issue with extract that is heavily tested by cake's core.

Or maybe you meant you see the user in the listener? If so, could you please debug($options) really early in the behavior's beforeSave() method?

jadb avatar Jan 29 '16 04:01 jadb

Here you go:

FootprintBehavior

 public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
    {
        debug($options);
        die();

And the output is:

object(ArrayObject) {
    atomic => true
    associated => [
        'projects' => [],
        'targetcompanies' => [],
        'createdby' => [],
        'cities' => [],
        'companiestarget' => [],
        'companies' => [],
        'contacttypes' => []
    ]
    checkRules => true
    checkExisting => true
    _primary => true
}

Just like in my very first comment.

P.S

My doubts are that FootprintListener's handleEvent is not called at all in the beforeSave

kristiyandobrev avatar Jan 29 '16 07:01 kristiyandobrev

The footprint listener is not used by the behavior, it listens to the controller. Am trying to grok this but all seems to be configured correctly.

I noticed you were pre-loading the models before setting up the Auth component which could have consequences. Any reason why you are _pre-_loading the models?

Have you been able to write a failing test?

jadb avatar Jan 29 '16 13:01 jadb

Any reason why you are pre-loading the models?

Yes I need the for later actions.

Also here is the output of $this-Auth->user();--> LINK

Could it be because the user is not a clear entity but it has some associations ?

kristiyandobrev avatar Jan 29 '16 13:01 kristiyandobrev

I can't grok the reason why it would fail. @ADmad any ideas?

jadb avatar Jan 29 '16 14:01 jadb

Nope.

ADmad avatar Jan 30 '16 19:01 ADmad

I'm having the same issue in my project, a little bit of investigation suggests the problem may be in CakePHP itself, as AuthComponent::_getUser() will short-circuit if the user can be retrieved from session storage without firing the Auth.afterIdentify event.

After adding the following on line 690 of AuthComponent, the event is now firing, and the current user gets as far as FootprintListener, but doesn't seem to be getting to the model.

$event = $this->dispatchEvent('Auth.afterIdentify', [$user]);

Going to keep investigating and see if I can find a fix

May be important, the model in question actually extends another model, so I have PostsTable which extends Cake\ORM\Table and QuestionsTable which extends App\Model\Table\PostsTable

Edit: the above doesn't seem to be supported in CakePHP from what I can see reading https://github.com/cakephp/cakephp/issues/4412

Second edit: Now seems to be working without me changing anything at all, might have been entirely due to the extending of the Table classes

mattford avatar Jan 31 '16 14:01 mattford

I also have this problem. I use FootprintBehavior for two tables, with the first it works great, with the second it doesn't.

I put a breakpoint here: https://github.com/UseMuffin/Footprint/blob/0f31ad6be0fbcff497ae8ccd07c1f862c595ebe9/src/Event/FootprintListener.php#L80

And another one here: https://github.com/UseMuffin/Footprint/blob/0f31ad6be0fbcff497ae8ccd07c1f862c595ebe9/src/Model/Behavior/FootprintBehavior.php#L119

What I expect: The first breakpoint fires at least once before the second breakpoint. (It is true for my table where it works)

What I observe: The second breakpoint fires first! Variable inspection reveals that data is missing. The first breakpoint then fires several times.

I really do nothing special, I call parent::initialize() before loading models and AppController has the trait (obviously, as it wouldn't work for the other table otherwise). My behavior configuration is dead simple:

Case 1 (works):

        $this->addBehavior('Muffin/Footprint.Footprint', [
            'events' => ['Model.beforeSave' => ['user_id' => 'new']],
        ]);

Case 2 (doesn't work):

        $this->addBehavior('Muffin/Footprint.Footprint', [
            'events' => ['Model.beforeSave' => [
                'owner_id' => 'new',
                'modifiedby_id' => 'existing'
            ]],
        ]);

Thanks for any hints on how to further debug this!

ypnos-web avatar Feb 02 '16 18:02 ypnos-web

Sorry everyone, really short on time, haven't looked into it yet.

If anyone can submit a failing test, it will definitely make things much easier ;)

/cc @ADmad @phillaf @deizel

jadb avatar Mar 20 '16 11:03 jadb

I have the same issue by calling $table = TableRegistry::get($tableName); in the Controller::beforeFilter() event. Sorry, but at the moment i do not have time to create a test case, but maybe this information helps.

stmeyer avatar Mar 21 '16 09:03 stmeyer