XF 2.1 Get ID from basicEntitySave

Dannymh

Active member
Hi all,

I am running a process that triggers a save into multiple tables and entities the idea being

  • go grab a bunch of jSON data
  • Create a week schedule
  • use the Week ID that was just created to create day data schedules

I can't figure out how I can get the ID of the data that was just inserted, so I really need to grab the record ID of newly inserted data and then pass that on to the next entity save process to save the days inside the week.

From what I can see should that not come out of the basicentitysave process?
 
FormAction objects take a bunch of closures and executes them when the run() method is called. Calling basicEntitySave() is just a shortcut for creating up setup, validate, and apply handlers for an entity.

You'd likely want to use a deferred value for the ID on the second entity:
PHP:
$second->first_id = $this->em()->getDeferredValue(
    function() use ($first) {
        return $first->first_id;
    },
    'save'
);

The value will then be resolved when the $second entity is saved.
 
FormAction objects take a bunch of closures and executes them when the run() method is called. Calling basicEntitySave() is just a shortcut for creating up setup, validate, and apply handlers for an entity.

You'd likely want to use a deferred value for the ID on the second entity:
PHP:
$second->first_id = $this->em()->getDeferredValue(
    function() use ($first) {
        return $first->first_id;
    },
    'save'
);

The value will then be resolved when the $second entity is saved.
Ok I think I get it.

Might need to look at doing this slightly differently as insert_id is all I mostly need but hopefully this works
 
I
FormAction objects take a bunch of closures and executes them when the run() method is called. Calling basicEntitySave() is just a shortcut for creating up setup, validate, and apply handlers for an entity.

You'd likely want to use a deferred value for the ID on the second entity:
PHP:
$second->first_id = $this->em()->getDeferredValue(
    function() use ($first) {
        return $first->first_id;
    },
    'save'
);

The value will then be resolved when the $second entity is saved.
It is often after a solid nights sleep that you sit down and realize what an a-grade moron you are. I was trying to grab this from the saveProcess when I could just put my code after the ->run and get this.....yup silly socks me, sorted now
 
when I could just put my code after the ->run and get this

Sorry, failing to understand this fully. My requirement is same - after basicEntitySave, the run() is called. But after this, how would I get the primary column (have named it as sys_id in table) value?
 
It will be updated on the entity that you fed into the basicEntitySave method. You can use the form->complete event to only execute your code snippet if the form saved successfully
 
Back
Top Bottom