# Wrong implementation of beforeSave event on my side?

**URL:** <https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769>\
**Category:** Need Help\
**Created:** [January 1, 2024, 3:30pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769 "2024-01-01T15:30:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![flyingjules](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flyingjules](https://discourse.cakephp.org/u/flyingjules)\
**Post date:** [January 1, 2024, 3:30pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/1 "2024-01-01T15:30:47Z")

</div>

I’m trying to overwrite the beforeSave Method in one of my Tables.  
Implemented the event as described in the docs, the function is however never reached/called.  
My code looks like this:

What am I missing?  
Do I need to tell the table somewhere explicitly to use this event?

```auto
    public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
    {
        /** @var \App\Model\Table\ApartmentsTable $apartmentsTable */
        /**@var \App\Model\Entity\RentalContract $entity */
        $apartmentsTable = TableRegistry::getTableLocator()->get('Apartments');
        $oldApartmentId = $entity->getOriginal('apartment_id');

        if ($entity->apartment_id) {
            $apartmentsTable->setApartmentUnavailable($entity->apartment_id);
        }

        if ($oldApartmentId) {
            $apartmentsTable->setApartmentAvailable($oldApartmentId);
        }

        die("x");
    }
```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 1, 2024, 4:56pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/2 "2024-01-01T16:56:59Z")

</div>

Check the type of the table object you’re using to save the entity, to make sure it’s the class you expect it to be.

---

<div class="post-metadata">

**Author:** ![flyingjules](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flyingjules](https://discourse.cakephp.org/u/flyingjules)\
**Post date:** [January 1, 2024, 8:23pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/3 "2024-01-01T20:23:29Z")

</div>

It definitely targets the correct Table (RentalContracts)

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/0cc494c8c3f6c761aa95e00727f7ab90adba7078.png)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 1, 2024, 10:21pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/4 "2024-01-01T22:21:16Z")

</div>

What does `debug(get_class($this->RentalContracts));` tell you?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 1, 2024, 10:21pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/5 "2024-01-01T22:21:58Z")

</div>

Any chance that it’s aborting the save before it gets to that event, due to validation errors?

---

<div class="post-metadata">

**Author:** ![flyingjules](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flyingjules](https://discourse.cakephp.org/u/flyingjules)\
**Post date:** [January 3, 2024, 4:25pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/6 "2024-01-03T16:25:35Z")

</div>

Found the problem. I’m using a Behavior in RentalContracts that I made myself which overrides the beforeMarshal Method. If I comment that out, it reaches the beforeSave method. I still don’t understand why that ignores the beforeSave method from the RentalContracts then. Got any clue what could cause that?  
 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/3/3367f3c67951db39024a2cbf4404427e5f8bd682.png)

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/5/58a00e42959116efd8891ac0915d3c7ad619a1a7.png)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 3, 2024, 10:53pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/7 "2024-01-03T22:53:03Z")

</div>

A function in a behavior should not “override” the function in the table. It should be called in addition to that other function. (I don’t recall what order they are called in; I’d guess it’s the table first, then any behaviors.)

You say your `beforeSave` is not reached. I am assuming that this means the entity is not saved. Is that correct? Like, your `->save(...)` call return false? Have you checked whether the entity has validation errors in it at that point (i.e. after the save fails)?

---

<div class="post-metadata">

**Author:** ![flyingjules](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flyingjules](https://discourse.cakephp.org/u/flyingjules)\
**Post date:** [January 4, 2024, 6:46am UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/8 "2024-01-04T06:46:02Z")

</div>

No, the entity is saved. Any modification of data that is done in the form is saved. Returns the correct Flash-\>success message as well. Override was maybe a bad choice of words. The beforeMarshal is implemented the same way in my behavior as in this example from the docs, just don’t have any defaultConfig properties and no slug function

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/b/b71098e3e86efb75f63fd20342523a5fc6bf44a2.png)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 4, 2024, 4:01pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/9 "2024-01-04T16:01:01Z")

</div>

So, just to confirm, the entity is successfully saved whether or not this behavior is included, but if it is included then the table’s `beforeSave` function never gets called, and if it isn’t included the table’s `beforeSave` function does get called?

---

<div class="post-metadata">

**Author:** ![flyingjules](https://avatars.discourse-cdn.com/v4/letter/f/8baadc/32.png) [@flyingjules](https://discourse.cakephp.org/u/flyingjules)\
**Post date:** [January 4, 2024, 5:57pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/10 "2024-01-04T17:57:14Z")

</div>

Correct as you described it

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 4, 2024, 6:26pm UTC](https://discourse.cakephp.org/t/wrong-implementation-of-beforesave-event-on-my-side/11769/11 "2024-01-04T18:26:11Z")

</div>

Well that’s just bizarre. I have no idea why that might happen. Looks like it’s a debugging exercise for you.

Try leaving the behavior in place, but remove everything in it? If that fixes it, add bits back until it breaks? Use xdebug to step through the save process and see if you can narrow down the problem?
