Can't submit form using create();

瘦欲@ 提交于 2019-12-13 04:52:34

问题


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

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

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


回答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.

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'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.

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

See also

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


来源:https://stackoverflow.com/questions/30174654/cant-submit-form-using-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!