更新CakePHP中的现有记录

时间:2016-06-14 21:30:35

标签: php cakephp

我想在CakePHP中创建以下内容:

起始页面:用户输入数据库中预先存在的登录名。表名loginname。 一旦用户输入了正确的登录名,用户就会继续进入注册表单,用户可以在该表单中将个人数据添加到现有记录中。

当用户保存表单时,只有登录名的记录会添加个人数据。

这就是我所拥有的:

起始页面的逻辑:

    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;
}

这是将数据保存到记录的逻辑:

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 {

        }
    }

我以为我通过函数checkCodeRespondent()得到了我的id,但那不起作用。我忘记了什么或我做错了什么?如果我需要提供更多信息,我很乐意提供帮助。

更新:我看到我需要添加echo $form->input('id');,只有当debug($this->request);的id值为空时才需要添加public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("XXX", "Scheduling MyJobService to run."); ComponentName serviceName = new ComponentName(this, MyJobService.class); JobInfo job = new JobInfo.Builder(MyJobService.JOB_ID, serviceName) .setBackoffCriteria(TimeUnit.SECONDS.toMillis(30), JobInfo.BACKOFF_POLICY_LINEAR) .setPeriodic(TimeUnit.SECONDS.toMillis(5)) .build(); JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); scheduler.cancel(MyJobService.JOB_ID); scheduler.schedule(job); } } public class MyJobService extends JobService { public final static int JOB_ID = 1000; @Override public boolean onStartJob(JobParameters params) { Log.d("XXX", "Job started"); jobFinished(params, true); return true; } @Override public boolean onStopJob(JobParameters params) { return true; } } 。我的代码需要更改什么?

1 个答案:

答案 0 :(得分:1)

如果您为id添加了隐藏字段,则只需替换:

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

人:

$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">
如果没有其他问题,

应该可以正常使用。