在Cake PHP中更新现有的sql数据

时间:2018-06-11 03:22:44

标签: php ajax post cakephp

目前正试图弄清楚如何更新cake php中的现有SQL数据。我的输入字段会自动填充预先存在的数据,但每当我点击保存更改现有数据时,它什么都不做。没有错误或警告信息。

这是我的控制器:

 public function settings()
{
    $user = $this->UserAuth->getUser();
    $userid = $this->UserAuth->getUserId();


    //$userEntity = $this->Push->find()->where(['user_id = ' => $userid])->order('time')->toArray($this->request->data, ['validate'=>false]);
    //$query = $this->Push->find('all', ['conditions' => ['Push.user_id' => $userid]])->last();

    $userEntity =  $this->Push->find('all', ['conditions' => ['Push.user_id' => $userid]])->last();


    if($this->request->is('patch', 'post', 'put')) {
        $errors = $userEntity->errors();
        if($this->request->is('ajax')) {
            if(empty($errors)) {
                $response = ['error'=>0, 'message'=>'success'];
            } else {
                $response = ['error'=>1, 'message'=>'failure'];
                $response['data']['push'] = $errors;
            }
            echo json_encode($response);exit;
        } else {
            if(empty($errors)) {
                        if(!empty($this->request->data($userEntity['iosapikey']))) {
                            $userEntity['iosapikey'] = $this->request->data['Push']['iosapikey'];
                        }
                        if(!empty($this->request->data['Push']['androidapikey'])) {
                            $userEntity['androidapikey'] = $this->request->data['Push']['androidapikey'];
                        }
                        if(!empty($this->request->data['Push']['restapikey'])) {
                            $userEntity['restapikey'] = $this->request->data['Push']['restapikey'];
                        }
                        if(!empty($this->request->data['Push']['segments'])) {
                            $userEntity['iosapikey'] = $this->request->data['Push']['segments'];
                        }
                        if(!empty($this->request->data['Push']['tags'])) {
                            $userEntity['androidapikey'] = $this->request->data['Push']['tags'];
                        }
                        if(!empty($this->request->data['Push']['deeplinks'])) {
                            $userEntity['restapikey'] = $this->request->data['Push']['deeplinks'];
                        }

                        if($this->Push->save($userEntity, ['validate'=>false])) {
                            $this->Flash->success(__('Push settings have been updated successfully'));
                            //$this->redirect(['action'=>'index', 'page'=>$page]);
                        } else {
                            $this->Flash->error(__('Unable to update push settings, please try again'));
                        }
                    }
                }
    } else {
                if(!empty($userEntity['iosapikey'])) {
                    $userEntity['iosapikey'] = $userEntity['iosapikey'];
                }
                if(!empty($userEntity['androidapikey'])) {
                    $userEntity['androidapikey'] = $userEntity['androidapikey'];
                }
                if(!empty($userEntity['restapikey'])) {
                    $userEntity['restapikey'] = $userEntity['restapikey'];
                }
                if(!empty($userEntity['segments'])) {
                    $userEntity['segments'] = $userEntity['segments'];
                }
                if(!empty($userEntity['tags'])) {
                    $userEntity['tags'] = $userEntity['tags'];
                }
                if(!empty($userEntity['deeplinks'])) {
                    $userEntity['deeplinks'] = $userEntity['deeplinks'];
                }
    }


    $this->set(compact('userEntity'));
    $this->set('_serialize', ['push']);


        } 

这是我的模板:

<div class="panel-body">
      <?php echo $this->element('Usermgmt.ajax_validation', ['formId'=>'editPushForm', 'submitButtonId'=>'editPushSubmitBtn']); ?>
      <?php echo $this->Form->create($userEntity, ['id'=>'editPushForm', 'class'=>'form-horizontal', 'type'=>'text']);?>
                <div class="form-group col-md-9 col-sm-10">
                    <label>iOS API Key:</label>
                    <div class="">
                        <?php echo $this->Form->input('Push.iosapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
                    </div>
                </div>
                <div class="form-group col-md-9 col-sm-10">
                    <label>Android API Key:</label>
                    <div class="">
                        <?php echo $this->Form->input('Push.androidapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
                    </div>
                </div>
                <div class="form-group col-md-9 col-sm-10">
                    <label>REST API Key:</label>
                    <div class="">
                        <?php echo $this->Form->input('Push.restapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
                    </div>
                </div>
                <div class="form-group col-md-9 col-sm-10">
                    <label>Segments:</label>
                    <div class="">
                        <?php echo $this->Form->input('Push.segments',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
                    </div>
                </div>
                <div class="form-group col-md-9 col-sm-10">
                    <label>Tags:</label>
                    <div class="">
                        <?php echo $this->Form->input('Push.tags',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
                    </div>
                </div>
                <div class="form-group col-md-9 col-sm-10">
                    <label>Deeplinks:</label>
                    <div class="">
                        <?php echo $this->Form->input('Push.deeplinks',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-3 col-sm-offset-2 col-sm-3">
                        <?php echo $this->Form->Submit(__('Save'), ['div'=>false, 'class'=>'btn btn-primary pushbutton', 'id'=>'editPushSubmitBtn']); ?>
                    </div>
                </div>
                <?php echo $this->Form->end();?>

我确定我在某个地方发现了一个小错误,但我已经花了很多时间没有出现任何积极的结果,我认为是时候寻找帮助了。

1 个答案:

答案 0 :(得分:-1)

Cake PHP查找函数返回一个对象,所以我会请求你使用像$ userEntity-&gt; iosapikey = $ this-&gt; request-&gt; data ['Push'] ['iosapikey']这样的语句;分配值并在cakephp https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#debugging-queries-and-resultsets

中启用debuging