user2314339
user2314339

Reputation: 393

Update existing record in CakePHP

I want to create the following in CakePHP:

Starting page: User enters a loginname which pre exists in the DB. Table name loginname. Once the user has entered the right loginname, the user continues to the registration form, where the user can add personal data to the existing record.

When the user saves the form, the record with only the loginname has personal data added.

This is what i have:

Logic for starting page:

    public function checkCodeRespondent() {

    //set var
    $password = $this->data['Respondent']['loginname'];

    //set condition for hasAny function
    $condition = array('Respondent.loginname'=>$password);

    //if password matches continue to registreer
    if($this->Respondent->hasAny($condition)){
        //find data by password
        $respondent = $this->findByPass($password);
        //set var for current id
        $id= $respondent['Respondent']['id'];
        //redirect with current id
        $this->redirect(array('action' => 'aanmelden', $id));
    } else {
    //if password does not match, return to index with errormessage
        $this->redirect(array('action' => 'index'));
    }
}
    public function findByPass($pass) {

    $respondent = $this->Respondent->find('first', array('conditions' => array('pass' => $pass)));
    return $respondent;
}

And this is the logic for saving the data to the record:

public function registreren() {
    if($this->request->isPost()) {

        $this->Respondent->id = $id;
        if ($this->Respondent->save($this->request->data)){
            $this->Flash->success(__('Your post has been saved.'));
            return $this->redirect(array('action' => 'index'));

        } else {

        }
    }

I thought i got my id through the function checkCodeRespondent() , but that's not working. What am i forgetting or what am i doing wrong? If i need to give more info, i'm happy to help ofcourse.

Update: I read that i need to add echo $form->input('id');, only when i debug($this->request); the id value is empty. What do i need to change in my code?

Upvotes: 1

Views: 1008

Answers (1)

Manohar Khadka
Manohar Khadka

Reputation: 2195

If you have added hidden field for id then just replace:

$this->Respondent->id = $id;

By:

$this->Respondent->id = $this->request->data['Respondent']['id'];
And in your view, give name="data[Respondent][id]" in the hidden field of id like:
<input type="hidden" name="data[Respondent][id]" value="value of id">

that should work fine for you if no other issues out there.

Upvotes: 1

Related Questions