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

Saturday, March 19, 2022

[FIXED] child parent problems with parameters php

 March 19, 2022     cakephp, cakephp-3.0, php, php-7     No comments   

Issue

This is what kind of edit method am I using inside the ChildController.

        public function edit($id = null)
        {
            parent::edit();
            $id = $this->request->data['id'];
            $company = $this->Companies->get($id, [
                'contain' => []
            ]);
            $this->set(compact('company'));
            $this->set('_serialize', ['company']);
        }

And this is what kind of method am I using inside the parent controller.

public function edit()
{
    $model = $this->getCurrentControllerName();
    $editEntity = $this->{$model}->newEntity();
    if ($this->request->is(['patch', 'put'])) {
        $entityData = $this->{$model}->patchEntity($editEntity, $this->request->data);
        if ($this->{$model}->save($entityData)) {
            $this->Flash->success(__('The entity has been saved.'));

            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The entity could not be saved. Please, try again.'));
        }
    }
}

Currently I have a situation in which when I edit, it 'posts' creates another record.

Scenarios that I have already tried:

  1. When I input this before calling the parent action then it gives me the right number. $id = $this->request->data['id']; But then when it goes to parent class it's gone and it says that it is a NULL.

  2. When I put it after calling the parent class it just deletes it and it says that it is a value 'NULL'.

  3. I have also tried to put it inside the parent::action public function edit($id) and with the return $id; with no luck. enter code here I have tried parameter ID to the edit in parent class. It is obvious to me that I am doing something wrong in the parent class, but I don't know what.

Of course, I want obviously to edit/update the only one record inside my application. What am I doing wrong ?


Solution

In your parent function, you aren't doing anything at all with the ID or the existing entity, so little wonder that it's not updating as you want it to.

Something like this, perhaps?

public function edit($id = null)
{
    $company = parent::_edit($id);
    if ($company === true) {
        return $this->redirect(['action' => 'index']);
    }

    // If you use the Inflector class, you could even move these lines into _edit,
    // and perhaps even eliminate this wrapper entirely
    $this->set(compact('company'));
    $this->set('_serialize', ['company']);
}

// Gave this function a different name to prevent warnings
// and protected access for security
protected function _edit($id)
{
    $model = $this->getCurrentControllerName();
    // No need to get the $id from $this->request->data, as it's already in the URL
    // And no need to pass an empty contain parameter, as that's the default
    $editEntity = $this->{$model}->get($id);

    if ($this->request->is(['patch', 'put'])) {
        $entityData = $this->{$model}->patchEntity($editEntity, $this->request->data);
        if ($this->{$model}->save($entityData)) {
            $this->Flash->success(__('The entity has been saved.'));
            return true;
        } else {
            $this->Flash->error(__('The entity could not be saved. Please, try again.'));
        }
    }

    return $editEntity;
}


Answered By - Greg Schmidt
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

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