无法使用 create() 提交表单;

Can't submit form using create();

所以我的里面有这个方法:

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

我对表格本身有这样的看法:

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(); 
?>

还有这个tableclass:

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',
        ]);
    }

}

当我提交它时,它给了我下一个错误:

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

不知道如何解决。 我还有一个 entity

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

那么我该如何解决这个出现在表单提交上的错误?

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

我想我需要做点什么with $this->set('job');?但我不确定具体是什么

按照惯例,控制器的默认可自动加载 table 是基于不带尾随 Controller 的控制器名称,因此对于 JobsController table class 命名为 Jobs(Table) 可以自动加载。

如果 table class 无法加载(例如因为它不存在,或者因为名称与从控制器名称派生的名称不匹配),魔法getter 处理这个将 return false,一个布尔值,这是你试图调用方法的地方,因此错误。

create() 顺便说一句,您应该查看 ORM migration guide 和一般文档,以了解现在的工作原理。

因此要么使用 $this->Jobs 并确保您有一个名为 JobsTable 的 table class,要么覆盖默认模型以使用 ( Controller::_setModelClass()), or load the desired table manually (TableRegistry::get() or Controller::loadModel()).

另见