PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 14, 2022

[FIXED] Cakephp 3 - newEntity/patchEnity not using supplied id

 February 14, 2022     cakephp, cakephp-3.0, php, validation     No comments   

Issue

I have a form in my CakePHP 3 project that edits user data. In my case an admin user can edit other users their roles.

When posting the form (actual request is a 'PUT' because of the edit), I get both the role and the id of the user.

The request data:

Array
(
    [role] => admin
    [id] => 2
)

This incoming data complete and correct. However, when I add the data to either '->newEntity' or '->patchEntity', the role is taken correctly, but id is just not added.

$user = $this->Users->newEntity($this->request->data, array('validate' => 'edit'));

$user = $this->Users->newEntity();
$user = $this->Users->patchEntity($user, $this->request->data, array('validate' => 'edit'));

In both case above I get the role, but not the id. Both give the following array:

Array
(
     [role] => admin
)

I use a custom validator, because only the id and role needs to validate for editing.

public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->requirePresence('username', 'create')
        ->notEmpty('username');

    $validator
        ->requirePresence('password', 'create')
        ->notEmpty('password');

    $validator
        ->allowEmpty('role');

    return $validator;
}

public function validationEdit(Validator $validator)
{
    $validator
        ->integer('id')
        ->requirePresence('id')
        ->notEmpty('id');

    $validator
        ->allowEmpty('role');

    return $validator;
}

I tried adding more restrictions to the 'edit' validator, but the id is never added regardless. The 'edit' validator is being used though. Changing the validate parameter to 'default' gives all the expected errors from the default validator, however the id is still not added.

Is there something obvious I am missing here? I should be some basic CakePHP stuff.


Solution

You don't need newEntity for editing existing record, If you are trying to edit role of user then just do:

$user = $this->Users->get($this->request->data['id']);
    if ($this->request->is(['patch', 'post', 'put'])) {
        $user = $this->Users->patchEntity($user, $this->request->data);
        if ($this->Users->save($user)) {
            // updated
        }
     }


Answered By - Manohar Khadka
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing