0

I have an EE (5.2.6) system to which I am adding new entries programmatically: the new entries are created during an AJAX call to an EE template that creates the new entries using php / EE Model via code like this:

$entry = ee('Model')->make('ChannelEntry');
$entry->title = '{get_post:title}';
$entry->url_title = '{get_post:url_title}';
$entry->site_id =  ee()->config->item('site_id');
$entry->author_id = ee()->session->userdata('member_id');
$entry->ip_address = ee()->session->userdata['ip_address'];
$entry->entry_date = ee()->localize->now;
$entry->edit_date = ee()->localize->now;
$entry->channel_id = 5;
$entry->status = 'open';
$entry->save();

$entry->field_id_5 = '{get_post:custom_content}';
$entry->save();

BTW - {get_post:...} is a tag from the mo'variables add-on that simply retrieves get or post variables with the name assigned in the tag

This creates the new entry (first save) and then loads some data into its custom fields (and then saves again).

The AJAX call uses the .done() method to delay subsequent processing until after the php activity is complete. Right now all that happens is a page reload (the page is generated by a template that lists out the entries in the channel concerned) - via code a bit like this:

function new_entry(url_title,title,custom_content) {
    return $.ajax({
        method: "GET",
        url: '/some/ee/template',
        data: {
            url_title: url_title,
            title: title, 
            custom_content: custom_content
        }
    });
}

new_entry(
    $url_title,
    $title,
    $custom_content
)
.done(location.reload(true));

This is all good, and works (it creates new entries etc.). Trouble is when the reload happens, the newly created entry does not appear in EE template output. If I manually reload the template moments later the entry does appear.

So I put some extra code into the php to dump out the state of play to a log as the code runs - and I can see from this that the new entry is being created OK, and I can even load the newly created entry into a variable within the php code and dump out the value in the custom content field of the newly created entry to the error log, all before processing of the php ends and return is passed back to javascript...

I have checked and template / tag caching is turned off across whole of EE templates and site. The reload(true) call should force browser not to reload from its cache either.

I'm clearly missing something... but no idea what!

Anyone got any thoughts about what I need to do for the newly created entris to appear when the reload happens?

Thanks in advance etc.

1

So it is a partial answer - it seems that the issue is managing the time it takes for EE to respond to the AJAX request. So for now I've fixed this by making the AJAX request synchronous... vis

function new_entry(url_title,title,custom_content) {
    return $.ajax({
        method: "GET",
        async: false,
        url: '/some/ee/template',
        data: {
            url_title: url_title,
            title: title, 
            custom_content: custom_content
        }
    });
}

Trouble is this is a deprecated setting, but so far my attempts to work around it using the recommended methods (which appears to be the promise / .then / .when construct) are not working well ... :(

So hopefully this is helpful to someone, but if anyone has a suggestion about how promise / then might be applied to this I'd love to see it.

| improve this answer | |
0

You might try structuring your ajax like this:

$.ajax( {
    url:        action,
    type:       'POST',
    dataType:   'html',
    data:       values,
    error:    function( jqXHR, textStatus, errorThrown  ) {
        alert( 'Sorry, there was an error' );
    },
    success:  function( result, html, textStatus, jqXHR ) {
        var obj = result;
        // do things with the result
        location.reload(true)
    }
});

I use ajax a lot with custom ee add-ons to query apis and such and this is how I do it.

Hope this helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.