CakePHP 3 - 无法添加新记录

时间:2017-08-17 11:31:17

标签: forms cakephp cakephp-3.x

当我提交新的工作表单时,hte操作会将所有信息传递给网址,但不会发生保存操作,也没有成功或错误的Flash消息。

在debugkit的变量选项卡中,除了id之外,所有看起来都没问题,但无论如何都要自动增加...

job (array)
    [new] (true) 
        [accessible] (array) 
            * (true)
            id (false)

编辑Job方法效果很好。

添加工作职能

public function add()
{
        $job = $this->Jobs->newEntity();
        if ($this->request->is('post')) {
            $job = $this->Jobs->patchEntity($job, $this->request->getData());
            $job->user_id = $this->Auth->user('id');
            if ($this->Jobs->save($job)) {
                $this->Flash->success(__('Job hasbeen saved'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Please try again'));            
        }

        $categories = $this->Jobs->Categories->find('list');
        $users = $this->Jobs->Users->find('list');
        $types = $this->Jobs->Types->find('list');
        $this->set(compact('job', 'categories', 'users', 'types'));
        $this->set('_serialize', ['job']);
    }   

查看

<div class="col_12 column">
    <form id="reg_form">    
        <fieldset>
            <legend><?= __('Add Job') ?></legend>
            <?php
                echo $this->Form->create($job);
                echo $this->Form->control('category_id', ['options' => $categories]);           
                echo $this->Form->control('user_id', ['options'=>$users]);
                echo $this->Form->control('type_id', ['options' => $types]);
                echo $this->Form->control('company_name');
                echo $this->Form->control('title');
                echo $this->Form->control('description');
                echo $this->Form->control('area');
                echo $this->Form->control('city');
                echo $this->Form->control('contact_email');
                echo $this->Form->button(__('Submit'));
                echo $this->Form->end();
          ?>
        </fieldset>   
    </form>
</div>

0 个答案:

没有答案