无法使用create()提交表单; [英] Can't submit form using create();

查看:157
本文介绍了无法使用create()提交表单;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有我的方法:


JobsController.ctp:

JobsController.ctp:



<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;

    /**
     * Jobs Controller
     *
     * @property \App\Model\Table\JobsTable $Jobs
     */
    class JobsController extends AppController
    {
        public $name = 'Jobs';

        public function add()
            {
                    //Some Vars assigning skipped, var job is empty
                    $this->set('job','Job');
                    $this->Job->create();
            }
    }

我有这个视图与窗体本身: / p>

And I have this view with the form itself:


add.ctp:

add.ctp:



<?= $this->Form->create($job); ?>
    <fieldset>
        <legend><?= __('Add Job Listing'); ?></legend>
        <?php 
            echo $this->Form->input('title');
            echo $this->Form->input('company_name');
            echo $this->Form->input('category_id',array(
                                    'type' => 'select',
                                    'options' => $categories,
                                    'empty' => 'Select Category'
                                    )); 
            echo $this->Form->input('type_id',array(
                                    'type' => 'select',
                                    'options' => $types,
                                    'empty' => 'Select Type'
                                    )); 
            echo $this->Form->input('description', array('type' => 'textarea'));
            echo $this->Form->input('city');
            echo $this->Form->input('contact_email');               
        ?>
    </fieldset>
<?php 
    echo $this->Form->button('Add');
    $this->Form->end(); 
?>

此表类也是:


JobsTable.php

JobsTable.php



<?php

namespace App\Model\Table;

use Cake\ORM\Table;

class JobsTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsTo('Types', [
            'foreignKey' => 'type_id',
            'joinType' => 'INNER',
        ]);
        $this->belongsTo('Categories', [
            'foreignKey' => 'category_id',
            'joinType' => 'INNER',
        ]);
    }

}

给我下一个错误:


错误:调用成员函数create()on boolean

Error: Call to a member function create() on boolean

不知道如何修复。
我还有一个实体


Job.php:

Job.php:



<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

/**
 * Job Entity.
 */
class Job extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * @var array
     */
    protected $_accessible = array(
        'category_id' => true,
        'user_id' => true,
        'type_id' => true,
        'company_name' => true,
        'title' => true,
        'description' => true,
        'city' => true,
        'contact_email' => true,
        'category' => true,
        'user' => true,
        'type' => true,
    );
}

那么如何解决在表单提交时出现的错误? p>

So how do I fix this error, that appears on form submit?


错误:调用成员函数create()on boolean

Error: Call to a member function create() on boolean

我想我需要做一些与$ this-> set('job'); ?但我不确定

I guess I need to do something with $this->set('job'); ? but I'm not sure what exactly

推荐答案

按照惯例,控制器的默认自动装载表是基于控制器名称不带尾随 Controller ,因此 JobsController 一个名为 Jobs Table )可以自动加载。

By convention the default, auto-loadable table for a controller is based on the controller name without the trailing Controller, so for JobsController a table class named Jobs(Table) can be autoloaded.

如果无法加载表类它不存在,或者因为名称与从控制器名称派生的名称不匹配),处理此的魔术getter将返回 false ,一个布尔值,这是您尝试调用方法的地方,因此会出现错误。

In case the table class cannot be loaded (for example because it doesn't exist, or because the name doesn't match the one derived from the controller name), the magic getter that handles this will return false, a boolean, and this is where you are trying to call a method on, hence the error.

create() btw doesn'已经存在,您应该查看 ORM迁移指南 和文档一般来了解现在的工作方式。

create() btw doesn't exist anymore, you should have a look at the ORM migration guide, and the docs in general to get a grasp on how things now work.

因此,请使用 $ this - > Jobs ,并确保您有一个名为 JobsTable 的表类,或覆盖要使用的默认模型( Controller :: _ setModelClass() )或载入手动选择所需表格( TableRegistry :: get() Controller :: loadModel () )。

So either use $this->Jobs and make sure that you have a table class named JobsTable, or override the default model to use (Controller::_setModelClass()), or load the desired table manually (TableRegistry::get() or Controller::loadModel()).

另请参阅

  • Cookbook > Database Access & ORM
  • Cookbook > Controllers > Loading Additional Models

这篇关于无法使用create()提交表单;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆