CakePHP's edit function doesn't work!!

Asked 2 years ago, Updated 2 years ago, 52 views

Nice to meet you!

I am trying to implement the edit profile screen using the edit function of CakePHP.

Use the following code to
①Information such as names and benefits already registered is not displayed on the editing screen.
②When saving, the user id(user_id) becomes "0", and the original user id is not stored.

The only difference from the normal edit code is that the Auth component utilizes the id data to be edited.

How can I function properly?
Thank you for your guidance.

//ProfilesController.php
   public function edit() {

     $user=AuthComponent::user('id');
     $this->Profile->user_id=$user;

     if($this->request->is('get')){
         $this->request->data=$this->Profile->read();// 編集The edit screen does not show information such as names and benefits that have already been registered.
     } else{ 
         If($this->Profile->save($this->request->data))){// 保存When saving, the user id(user_id) becomes "0", and the original user id is not stored.
             $this->Session->setFlash('Success!');
             $this->redirect(array('action'=>'index'));
         } else{
             $this->Session->setFlash('Failed!');
         }
     }
   }

//edit.ctp
<?php
echo$this->Form->create('Profile', array('action'=>'edit'));
echo$this->Form->input('name');
echo$this->Form->input('merit');
echo$this->Form->input('career');
echo$this->Form->end('Save');
?>

php cakephp

2022-09-30 19:28

1 Answers

The first Profile data should be loaded as follows:

$profile=$this->Profile->find('first', ['Profile.user_id'=>$user_id]);

And (1)

$this->request->data=$profile;

Yes, in (2),

$this->request->data['Profile']['user_id']=$profile['Profile']['user_id'];

should be

(1)
Model::read() does not perform delayed reads.
Instead, I think false is returned.

Model::read() must be the first argument or Model::$id.
You are supposed to return false without doing anything.

$this->request->data is false, so it is not used in $this->Form

(2)
Model::save() gets Model::$id first and
But I don't have $this->request->data['Profile']['user_id'], so
As a result, 'user_id' in the row of the id is saved as 0.

Did you know 'user_id' in $this->Form from 'hidden' or something in the sample?
Didn't you force a replacement just before Model::save() as above?


2022-09-30 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.