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

时间:2022-11-23 20:31:26

So i have this method inside of my:

所以我在我的内部有这个方法:

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

And I have this view with the form itself:

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

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

Also this table class:

这个表类也是:

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

}

And when I submit it, it gives me next error:

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

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

错误:在布尔值上调用成员函数create()

No idea how to fix. I also have an 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,
    );
}

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

那么如何修复表单提交中出现的错误?

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

错误:在布尔值上调用成员函数create()

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

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

1 个解决方案

#1


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.

按照惯例,控制器的默认可自动加载表基于控制器名称而没有尾随Controller,因此对于JobsController,可以自动加载名为Jobs(Table)的表类。

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.

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

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.

create()btw不再存在,你应该看一下ORM迁移指南,以及一般的文档来掌握现在的工作方式。

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()).

所以要么使用$ this-> Jobs并确保你有一个名为JobsTable的表类,要么覆盖要使用的默认模型(Controller :: _ setModelClass()),要么手动加载所需的表(TableRegistry :: get()或控制器:: loadModel())。

See also

#1


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.

按照惯例,控制器的默认可自动加载表基于控制器名称而没有尾随Controller,因此对于JobsController,可以自动加载名为Jobs(Table)的表类。

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.

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

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.

create()btw不再存在,你应该看一下ORM迁移指南,以及一般的文档来掌握现在的工作方式。

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()).

所以要么使用$ this-> Jobs并确保你有一个名为JobsTable的表类,要么覆盖要使用的默认模型(Controller :: _ setModelClass()),要么手动加载所需的表(TableRegistry :: get()或控制器:: loadModel())。

See also