CakePhp 3.6: Update data after post with patchEntity does not work with ajax


I would like to update an entity with some “calulated” data like so:

$data = [
‘key’ => 100,

$entity = $this->Table->patchEntity($entity, $data);

The form helper should reflect the updated data after post. This works perfectly after a regular http post, but not with ajax post and I have no idea why.

Any ideas?

$this->request->data[‘key’] = 100 works, but this is a bad habbit and deprecated in 3.6 at all.


Ok, Ive looked into EntityContext and it seems the requestData has priority over the enttity data (requestData returns first place in the val() method).

So the updated entity value will not be shown in the form. Modifying the requestData is bad practice and deprecated.

So what now? Update every input control with a value="" attribute and a “custom” array? Can’t believe there is no other way…

Or is possible to tell the Form::create method not to use the requestData for default values and instead the entity like in a “non-posted-form”?

Do you means, you want to declare the data before or after saved?

You can try


public function beforeSave(Event $event, Entity $entity)
    if($entity->isNew()) {       
        //on create
    } else {
        //on update

Without saving :wink:

After post via ajax. Some input is based on calculation and the result should be updated to the form field after ajax submit. It does work on a “regular” post but not with ajax.

How you return result for ajax in controller?
If you return JSON format, so you can render json_encode(Array);
The json_encode result can read after ajax success.

This example code just idea not real code.


    url : '../brands/add',
    data : {
        name : "test",
        shortname : "tst"
    dataType : 'json',
    success : function(data) {
      //here you can write update form
    error : function(data) {
      console.log("Eorror occured");


public function add() {
  $this->autoRender = false;     
  if (this->request->is('ajax') && this->request->is('post') ){
    $res = [
        'data' => [
             /* your data */
    return $this->response;

No problem in reading the ajax data :wink:

The problem I cannot set updated data to the view (ajax html response which will be insertet into the form).

If you get any response from an ajax call you should use JavaScript to update any input data.

Like rrd said, if you get the ajax response or the returned value of your controller, you should be able to manipulate the DOM object using javascript. With that said, I don’t think is a CakePHP issue.

Now to pass the result of your logic from your controller you have to set the variables first and then echo encode those variables to get them ready for your javascript.

$this->set(compact(‘var1’, ‘var2’));
echo json_encode(compact(‘var1’, ‘var2’));

Now in your ajax, assuming you are using jQuery, you have to read that json encoded data and in order to manipulate your DOM.

success: function(data){
var variable1 = .parseJSON(data).var1; var variable2 = .parseJSON(data).var2;

And voila !! you got the value to update at you own taste the DOM via javascript.

Hope I answer the question !