Tachi
Tachi

Reputation: 2202

Can't submit form using create();

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

Upvotes: 0

Views: 1188

Answers (1)

ndm
ndm

Reputation: 60453

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

Upvotes: 2

Related Questions