# Question about saveAssociates

**URL:** <https://discourse.cakephp.org/t/question-about-saveassociates/10032>\
**Category:** Need Help\
**Created:** [December 27, 2021, 9:46pm UTC](https://discourse.cakephp.org/t/question-about-saveassociates/10032 "2021-12-27T21:46:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgrinter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bgrinter/32/1381_2.png) [@bgrinter](https://discourse.cakephp.org/u/bgrinter)\
**Post date:** [December 27, 2021, 9:46pm UTC](https://discourse.cakephp.org/t/question-about-saveassociates/10032/1 "2021-12-27T21:46:19Z")

</div>

Is there a way with an associated model to determine whether a save was triggered by the model itself, or its parent?

So for example - I have a “Member” who HasMany “MemberStatuses”

During the initial create, the “Member” and the initial “MemberStatus” is created. Subsequently I can add a new “MemberStatus”. Can I ascertain whether the MemberStatus save was create as a result of a Member save with associated models?

Reason I ask is I need to determine the in the afterSave for the “MemberStatuses” and have different logic accordingly.

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [December 28, 2021, 2:54am UTC](https://discourse.cakephp.org/t/question-about-saveassociates/10032/2 "2021-12-28T02:54:56Z")

</div>

Possibly with a created\_at timestamp.

---

<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:** [December 28, 2021, 3:56am UTC](https://discourse.cakephp.org/t/question-about-saveassociates/10032/3 "2021-12-28T03:56:06Z")

</div>

I can’t remember the specifics right now, but I think there’s something added to the `$options` that get passed to the various event handlers (e.g. afterSave) that indicates whether the operation is happening for the top-level entity or an association.

---

<div class="post-metadata">

**Author:** ![bgrinter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bgrinter/32/1381_2.png) [@bgrinter](https://discourse.cakephp.org/u/bgrinter)\
**Post date:** [December 28, 2021, 6:30am UTC](https://discourse.cakephp.org/t/question-about-saveassociates/10032/4 "2021-12-28T06:30:52Z")

</div>

Cool thanks - found there’s a `_sourceTable` property in `$options` that tells me the source table instance for the save event, and also a `_primary` property - can’t find too much documentation on this other than it’s set to false on a save via association

Sometimes it seems like a lot of what I do is

```auto
        debug($options);
        die();

```

“oh ok - now I see what it’s doing…” 😛

---

<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:** [December 28, 2021, 4:26pm UTC](https://discourse.cakephp.org/t/question-about-saveassociates/10032/5 "2021-12-28T16:26:51Z")

</div>

`_primary` is what I was thinking of, but depending on your use case, `_sourceTable` might be more appropriate. Or, you might want to add your own custom option when saving with one method vs the other and check for that.

`debug($options);` is super useful, until you get `xdebug` working, and then you wonder why you wasted so much time with `debug()` statements. 🙂

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [December 28, 2021, 9:22pm UTC](https://discourse.cakephp.org/t/question-about-saveassociates/10032/6 "2021-12-28T21:22:16Z")

</div>

I add options manually  
`'$this->save($var,['skipAfterSave' => true]); `  
and have  
`if (isset($options['skipAfterSave'])) return;`  
at beginning of my model events.

---

<div class="post-metadata">

**Author:** ![bgrinter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bgrinter/32/1381_2.png) [@bgrinter](https://discourse.cakephp.org/u/bgrinter)\
**Post date:** [December 28, 2021, 11:54pm UTC](https://discourse.cakephp.org/t/question-about-saveassociates/10032/7 "2021-12-28T23:54:28Z")

</div>

`_primary` seems to have done the trick

Checking out **xdebug** is on the list of things to do during the holiday break, heard many good things about it, although also heard tricky to set up.

Lots of `debug('got to here');` in a pinch when you’re lazy 😉

Worked with a French speaking programmer last century, you would find `PRINT "MERDE"` sprinkled through his code. Occasionally he left it in code that shipped and I’d get a call from a client “Program has hung and there’s an error code on screen, M-E-R-D-E. What does that mean?”

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [January 5, 2022, 10:26am UTC](https://discourse.cakephp.org/t/question-about-saveassociates/10032/8 "2022-01-05T10:26:59Z")

</div>

change it to `PRINT "MERDE ". __LINE__ ` and it will be good 😉
